您好,我无法在我的项目中禁用 CORS。我使用自定义过滤器和 Spring Security Config 进行 CORS 配置。我看到了这个很棒的答案:可以在 Spring 中完全禁用 CORS 支持吗? https://stackoverflow.com/questions/44697883/can-you-completely-disable-cors-support-in-spring
但是当我尝试以下实现时,我仍然收到 CORS 错误:
CORS配置:
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
class CorsFilter @Autowired
constructor() : CorsFilter(configSrc()) {
companion object {
private fun configSrc(): UrlBasedCorsConfigurationSource {
val config = CorsConfiguration()
config.allowCredentials = true
config.addAllowedOrigin("http://127.0.0.1:3000")
config.addAllowedHeader("*")
config.addAllowedMethod("*")
val src = UrlBasedCorsConfigurationSource()
src.registerCorsConfiguration("/**", config)
return src
}
}
}
我还尝试将允许的原点设置为如下所示,但没有结果:
config.addAllowedOrigin("http://127.0.0.1:3000")
这些是来自正在进行的 OPTIONS 请求的响应标头:
This is the exact error I am getting:
您能否指出任何其他想法或为什么会发生这种情况?我认为这将是一个很容易解决的问题,但它最终消耗了我大量的时间。
谢谢
您可以尝试通过以下方式在应用程序类中添加 CORS 映射:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/v1/**")
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowCredentials(true)
.maxAge(3600);
}
};
}
https://spring.io/guides/gs/rest-service-cors/ https://spring.io/guides/gs/rest-service-cors/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)