Spring Boot通过application.properties启用CORS
问题描述:
我使用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");
}
};
}