C96*_*C96 10 java spring cors kotlin spring-boot
嗨,我无法在我的项目中禁用 CORS。我对 CORS 配置使用自定义过滤器和 Spring Security Config。我看过这个很好的答案:你能在 Spring 中完全禁用 CORS 支持吗?
但是当我尝试以下实现时,我仍然收到 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
}
}
Run Code Online (Sandbox Code Playgroud)
}
我还尝试将允许的原点设置为如下所示,但没有结果:
config.addAllowedOrigin("http://127.0.0.1:3000")
Run Code Online (Sandbox Code Playgroud)
这些是来自正在进行的 OPTIONS 请求的响应标头:
您能否指出任何其他想法或为什么会发生这种情况?我认为这将是一个简单的解决问题,但它最终消耗了我很多时间。
谢谢
您可以尝试通过以下方式在应用程序类中添加 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);
}
};
}
Run Code Online (Sandbox Code Playgroud)
https://spring.io/guides/gs/rest-service-cors/
| 归档时间: |
|
| 查看次数: |
655 次 |
| 最近记录: |