我需要在我的Spring Boot Web应用程序中添加CORS过滤器。
我已经按照以下文档描述的方式添加了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头以避免此问题?
addFilterBefore
对我很有用。谢谢。在尝试了3个小时后,您的解决方案对我起作用了。 - biniam