Spring Boot通过application.properties启用CORS [英] Spring Boot enabling CORS by application.properties
问题描述
我使用Spring Boot API RESTful,该实体由Entities类自动启动。我正在从前端Web应用程序中使用此apiRest,但它给了我这个错误:
I using Spring Boot API RESTful that get up automatically by your Entities Class. I'm consuming this apiRest from a front-end web app but it gives me this error:
否'Access-Control-Allow- Origin的标头出现在请求的资源上
No 'Access-Control-Allow-Origin' header is present on the requested resource
我正在使用指定的applicantion.properties设置CORS配置此处。
I'm setting the CORS configuration using the applicantion.properties specified here.
我的基本配置是:
endpoints.cors.allow-credentials=true
endpoints.cors.allowed-origins=*
endpoints.cors.allowed-methods=*
endpoints.cors.allowed-headers=*
我在这些变量中尝试了不同的组合,但仍然无法正常工作。有任何想法吗?
I have tried different combinations in those variables but still not working. Any ideas?
推荐答案
我自己得到了答案:
只需将其添加到application.java
Just add this to application.java
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/greeting-javaconfig").allowedOrigins("http://localhost:9000");
}
};
}
这篇关于Spring Boot通过application.properties启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!