大佬教程收集整理的这篇文章主要介绍了迁移到Spring Boot 2.0.x时,全局CORS配置中断,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Spring文档和许多示例都缺少此功能,但是答案非常简单。我刚刚在CorsRegistry上看到了allowCredentials()方法,并向注册表方法链中添加了.allowCredentials(true),并重新添加了Access- Control-Allow-Credentials标头。
另外,我不再使用不推荐使用的WebMvcConfigurerAdapter,而是现在实现WebMvcConfigurer并重写addCorsmapPings()方法。
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@OverrIDe
public voID addCorsmapPings(CorsRegistry registry) {
registry.addMapPing("/**")
.allowedOrigins(
"http://localhost:3000",..)
.allowedMethods("GET", "POST", "PUT", "deletE", "head")
.allowCredentials(true)
;
}
}
为什么在Spring Boot
2.0.x(我的情况是2.0.1RELEASE)下,不再响应预检调用(OPTIONS)而发送“访问控制允许凭据”?这是我的Global
CORS配置,在Spring Boot 1.5.6下可以正常工作:
@Configuration
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsmappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(
"http://localhost:3000",..)
.allowedMethods("GET","POST","PUT","deletE","HEAD");
}
};
}}
我的pom依赖项(我正在做自己的安全性并避免使用Spring Security):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
我对REST端点的服务调用使预检失败:
我已经验证了在Spring Boot 1.5.6的情况下确实存在“ Access-Control-Allow-Credentials”标头,而在Spring
Boot 2.0.1下却不存在。
所有的文件,我可以找到,包括spring.io最新这里,说我的全局配置仍然是正确的,即使WebMvcConfigurerAdapter现在似乎被弃用。
更新:
以下是迁移前后的响应标头:
迁移之前(Spring Boot 1.5.6):
迁移后(Spring Boot 2.0.1-缺少Access-Control-Allow-Credentials标头,但其他值已更改/添加):
以上是大佬教程为你收集整理的迁移到Spring Boot 2.0.x时,全局CORS配置中断全部内容,希望文章能够帮你解决迁移到Spring Boot 2.0.x时,全局CORS配置中断所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。