【发布时间】:2018-10-15 12:23:09
【问题描述】:
为什么不再发送我的“Access-Control-Allow-Credentials”以响应 Spring Boot 2.0.x(在我的情况下为 2.0.1.RELEASE)下的预检调用 (OPTIONS)?这是我在 Spring Boot 1.5.6 下运行良好的全局 CORS 配置:
@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 端点的服务调用未能通过预检:
无法加载 http://localhost:8080/api/v5/sec/auth:对预检请求的响应未通过访问控制检查:“Access-Control-Allow-Credentials”标头中的值当请求的凭据模式为“包含”时,响应为“”,必须为“真”。因此不允许访问 Origin 'http://localhost:3000'。
我已验证在 Spring Boot 1.5.6 中确实存在“Access-Control-Allow-Credentials”标头,而在 Spring Boot 2.0.1 中则缺失。
我能找到的所有文档,包括 spring.io here 上的最新文档,都表明我的全局配置仍然正确,尽管 WebMvcConfigurerAdapter 现在似乎已被弃用。
更新:
这里是迁移前后的响应头:
迁移前(Spring Boot 1.5.6):
访问控制允许凭据:true
访问控制允许来源:http://localhost:3000
内容类型:application/json;charset=UTF-8
日期:日,日,星期一 yyyy hh:mm:ss GMT
传输编码:分块
变化:起源
迁移后(Spring Boot 2.0.1 - Access-Control-Allow-Credentials 标头缺失,但其他已更改/添加):
访问控制允许标题:内容类型
Access-Control-Allow-Methods: GET,HEAD,POST
Access-Control-Allow-Origin: *
访问控制最大年龄:1800
内容长度:0
日期:日,日,星期一 yyyy hh:mm:ss GMT
变化:起源
变化:访问控制请求方法
变化:访问控制请求标头
【问题讨论】:
标签: spring-boot cors