【问题标题】:Change of CORS policy in spring boot version 2.4.0spring boot 2.4.0 版本中 CORS 策略的变化
【发布时间】:2021-03-01 18:19:27
【问题描述】:

使用 Spring 2.3.0.RELEASE 我得到了以下 CORS 确认:

@Configuration
@EnableWebSecurity
@ComponentScan("com.softeq.ems.config")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class EmsJwtSecurityConfig extends BaseSecurityConfig {

    @Value("${management.endpoints.web.cors.allowed-origins}")
    private String[] allowedOrigins;

    @Override
    protected void configureHttp(HttpSecurity http) throws Exception {
        if (allowedOrigins.length > 0) {
            http.cors().configurationSource(corsConfigSource());
        }

        http.csrf().disable();
    }

    private CorsConfigurationSource corsConfigSource() {

        final CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.addAllowedHeader(CorsConfiguration.ALL);
        corsConfig.addAllowedMethod(CorsConfiguration.ALL);

        Stream.of(allowedOrigins).forEach(
            origin -> corsConfig.addAllowedOrigin(origin)
        );

        return request -> corsConfig;
    }

变量management.endpoints.web.cors.allowed-origins = http://localhost:4200, http://127.0.0.1:4200

此配置运行良好,我需要的所有跨平台请求都已获得授权。

但是在发布后迁移到spring-boot 2.4.0后,当我尝试像往常一样向主机发送请求时,在chrome浏览器控制台中遇到了经典的cors policy错误:

Access to XMLHttpRequest at 'http://localhost:8080/api/v1/me/balance' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status

Spring 发布说明说 cors 配置提供了一个新属性 allowedOriginPatterns,但我不明白如何使用它: https://github.com/spring-projects/spring-framework/wiki/What%27s-New-in-Spring-Framework-5.x#general-web-revision

请帮我弄清楚我的问题是什么!

【问题讨论】:

    标签: java spring spring-boot cors


    【解决方案1】:

    我会对你的代码做什么:

    private CorsConfigurationSource corsConfigSource() {
    
        final CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.addAllowedHeader(CorsConfiguration.ALL);
        corsConfig.addAllowedMethod(CorsConfiguration.ALL);
    
        Stream.of(allowedOrigins).forEach(
            //origin -> corsConfig.addAllowedOrigin(origin)
            origin -> corsConfig.addAllowedOriginPattern(origin)
        );
    
        return request -> corsConfig;
    }
    

    【讨论】:

    • 这对我有用,谢谢!自从a以来特别烦人。该更改仅在发行版中提及,并未简要提及,但没有任何迹象表明这是一项重大更改 b. addAllowedOrigin 方法仍然存在,并且该方法的 javadoc 开头说您仍然可以将它与通配符 * 一起使用。虽然之后解释了如果 setAllowCredentials(true) 应该使用 addAllowedOriginPattern,但这很容易忽略
    【解决方案2】:

    我是这样做的:

    @Configuration
    @Profile("!production")
    class CorsConfig : WebMvcConfigurer {
        override fun addCorsMappings(registry: CorsRegistry) {
            registry
                .addMapping("/**")
                .allowedOriginPatterns("http://localhost:3000")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-07
      • 2021-12-19
      • 2021-08-14
      • 2019-08-15
      • 2021-10-13
      • 1970-01-01
      • 2021-03-02
      • 2021-01-23
      • 2021-11-14
      相关资源
      最近更新 更多