迁移到Spring Boot 2.0.x时,全局CORS配置中断

时间:2018-05-05 00:43:35

标签: spring-boot cors

为什么我的“访问控制 - 允许 - 凭据”和#39;在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");
        }
    };
}}

我的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:对预检请求的响应未通过访问控制检查:“访问控制 - 允许 - 凭据”的值为&#39;回复中的标题是&#39;&#39;哪一定是真的&#39;当请求的凭据模式为&#39; include&#39;时。起源&#39; http://localhost:3000&#39;因此不允许访问。

我已经验证了“访问控制 - 允许 - 凭据”&#39;在Spring Boot 1.5.6的情况下确实存在头,在Spring Boot 2.0.1下缺少。

我能找到的所有文档,包括spring.io here的最新文档,都表示我的全局配置仍然正确,即使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
  日期:Day,dd Mon yyyy hh:mm:ss GMT
  转移编码:分块
  变化:起源

迁移后(Spring Boot 2.0.1 - 缺少Access-Control-Allow-Credentials标头,但其他人已更改/添加):

  

Access-Control-Allow-Headers:content-type
  Access-Control-Allow-Methods:GET,HEAD,POST &lt; - 我指定的方法被忽略
  Access-Control-Allow-Origin:* &lt; - 我指定的来源被忽略
  Access-Control-Max-Age:1800
  内容长度:0
  日期:Day,dd Mon yyyy hh:mm:ss GMT
  变化:起源
  变化:访问控制请求方法
  变化:访问控制请求标头

5 个答案:

答案 0 :(得分:18)

Spring文档和许多示例中都缺少这个,但答案很简单。我刚刚在CorsRegistry上看到了allowCredentials()方法,并将.allowCredentials(true)添加到了注册表方法链,并且重新添加了Access-Control-Allow-Credentials标头。

此外,我不再使用已弃用的WebMvcConfigurerAdapter,但现在实现WebMvcConfigurer并覆盖addCorsMappings()方法。

List<T> myList = context.Set<T>().ToList();

答案 1 :(得分:2)

如果您使用的是Spring Boot 2.0.x

CORS支持默认情况下处于禁用状态,并且仅在设置了management.endpoints.web.cors.allowed-origins属性后才启用。以下配置允许来自example.com域的GET和POST调用:

  

management.endpoints.web.cors.allowed-origins = http://example.com   management.endpoints.web.cors.allowed-methods = GET,POST

有关更多信息,refer

答案 2 :(得分:1)

第1步:即使您可以将自己的CorsFilter注册为Bean来提供自己的配置,Spring也已经具有CorsFilter。

@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowedOrigins(Collections.singletonList("http://localhost:3000")); // Provide list of origins if you want multiple origins
    config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept"));
    config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
    config.setAllowCredentials(true);
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}

第2步:使用@CrossOrigin注释对控制器进行注释。

答案 3 :(得分:0)

我正在使用Spring Boot 2.0.2。我有同样的问题,但是我使用以下代码对其进行了修复。有人有最好的方法吗?

//    Miss `Access-Control-Allow-Origin` header in response using this bean. 
//    @Bean
//    CorsConfigurationSource corsConfigurationSource() {
//        CorsConfiguration configuration = new CorsConfiguration();
//        configuration.setAllowCredentials(true);
//        configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
//        configuration.addAllowedMethod("*");
//        configuration.setAllowedOrigins(this.getAllowedOrigins());
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", configuration);
//        return source;
//    }

    @Bean
    public FilterRegistrationBean<CorsFilter> initCorsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
        config.addAllowedMethod("*");
        config.setAllowedOrigins(this.getAllowedOrigins());
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }

答案 4 :(得分:0)

这对我有用(科特琳):

@Configuration
class CorsConfig : WebMvcConfigurer {
    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**")
    }
}