为什么我的'Access-Control-Allow-Credentials'不再被发送以响应Spring Boot 2.0.x(在我的情况下为2.0.1.RELEASE)下的预检调用(OPTIONS)?这是我的全局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");
}
};
}}
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
我对REST端点的服务调用未通过预检:
无法加载http:// localhost:8080/api/v5/sec/auth:对预检请求的响应未通过访问控制检查:响应中的"Access-Control-Allow-Credentials"标头的值为' '当请求的凭据模式为'include'时,必须为'true'.因此不允许来源' http:// localhost:3000 '访问.
我已经验证了在Spring Boot 1.5.6的情况下确实存在'Access-Control-Allow-Credentials'标题,并且在Spring Boot 2.0.1下缺少.
所有的文件,我可以找到,包括spring.io最新这里,说我的全局配置仍然是正确的,即使WebMvcConfigurerAdapter现在似乎被弃用.
更新:
以下是迁移前后的响应标头:
迁移之前(Spring Boot 1.5.6):
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http:// localhost:3000
Content-Type:application/json; charset = UTF-8
Date:day,dd Mon yyyy hh:mm:ss GMT
Transfer-Encoding:chunked
Vary:Origin
迁移后(Spring Boot 2.0.1 - Access-Control-Allow-Credentials标头丢失,但其他人更改/添加):
Access-Control-Allow-Headers:content-type
Access-Control-Allow-Methods:GET,HEAD,POST < - 忽略我指定的方法
Access-Control-Allow-Origin:* < - 我的指定原点被忽略
Access-Control -Max-Age:1800
内容长度:0日期
:Day,dd Mon yyyy hh:mm:ss GMT
Vary:Origin
Vary:Access-Control-Request-Method
Vary:Access-Control-Request-Headers
sam*_*usa 30
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)
;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13781 次 |
| 最近记录: |