【问题标题】:Spring CORS Filter problem in light of Spring Cloud microservices with Zuul基于 Spring Cloud 微服务与 Zuul 的 Spring CORS 过滤器问题
【发布时间】:2019-05-26 05:35:23
【问题描述】:

Given:Angular 前端应用通过网关微服务向后端微服务发送请求。后端在 Spring Cloud 中。

问题:如何正确配置CORS过滤器以摆脱以下错误:

Access to XMLHttpRequest at 'http://gateway-service:5555/api/useful-service/myentities/' from origin 'http://localhost:4200' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:4200, http://localhost:4200', but only one is allowed.

这就是我到目前为止所写的:

网关服务

我在网关服务中的主类有 3 个注解:@SpringBootApplication、@EnableZuulProxy 和 @Configuration。因此,由于我没有配置任何安全性,因此我认为没有使用 Spring Security,因此我需要配置 Spring MVC 的 CorsFilter。我就是这样做的(cmets 是为未来的搜索者准备的)

@Bean
public CorsFilter corsFilter() {
    CorsConfiguration corsConfig = new CorsConfiguration();
    corsConfig.setAllowCredentials(true);
    //corsConfig.addAllowedOrigin("http://localhost:4200");
    corsConfig.addAllowedOrigin("*"); //wildcard that will simply copy the value of the request's Origin header
    // into the value of the Response's Access-Control-Allow-Origin header, effectively allowing all origins.
    // You can add specific origins instead if you wish to limit them.
    corsConfig.addAllowedHeader("*");
    corsConfig.addAllowedMethod("OPTIONS");
    corsConfig.addAllowedMethod("HEAD");
    corsConfig.addAllowedMethod("GET");
    corsConfig.addAllowedMethod("POST");
    corsConfig.addAllowedMethod("PUT");
    corsConfig.addAllowedMethod("DELETE");
    corsConfig.addAllowedMethod("PATCH");
    UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
    configSource.registerCorsConfiguration("/**", corsConfig);
    return new CorsFilter(configSource);
}

有用的服务

这里的主类使用@EnableResourceServer 和@SpringBootApplication 注解。根据我的“业务规则”,我希望拥有 Spring 授权(url 安全性,以及将来的方法安全性),因此我通常配置 Spring Security 和 OAuth2,特别是我应该配置安全性的 cors 过滤器。以下是启用 cors 的相关安全 sn-p:

@Configuration
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .cors(); // by default uses a Bean by the name of corsConfigurationSource
    }
}

这就是我配置 spring security 的 cors 功能的方式:

@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("*"));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
    configuration.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

不幸的是,我收到了上面提到的错误,如果您知道如何解决它,请讲述。

【问题讨论】:

    标签: spring spring-boot spring-security cors


    【解决方案1】:

    似乎这个问题已通过 DedupeResponseHeader-filter 得到解决。 见https://github.com/spring-cloud/spring-cloud-gateway/pull/866

    【讨论】:

    猜你喜欢
    • 2016-07-11
    • 2020-04-15
    • 2018-08-23
    • 2015-01-18
    • 2023-03-04
    • 2017-11-21
    • 2016-06-21
    • 2018-08-06
    • 2015-07-31
    相关资源
    最近更新 更多