【发布时间】:2016-08-17 00:16:41
【问题描述】:
我需要将 CORS 过滤器添加到我的 Spring Boot Web 应用程序中。
我已添加 CORS 映射,如以下文档 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html 中所述
这是我的配置:
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
// @formatter:off
registry
.addMapping("/**")
.allowedOrigins(CrossOrigin.DEFAULT_ORIGINS)
.allowedHeaders(CrossOrigin.DEFAULT_ALLOWED_HEADERS)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(3600L);
// @formatter:on
}
...
}
现在当我尝试访问我的 API 时收到以下错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com/api/v1.0/user. (Reason: CORS preflight channel did not succeed).
这是FF控制台的截图:
我做错了什么以及如何正确配置 CORS 标头以避免此问题?
【问题讨论】:
标签: spring spring-boot spring-mvc spring-security cors