Zuul代理CORS标头包含多个值,标头重复两次-Java Spring Boot CORS过滤器配置

肯特·布尔

为什么我要让每个CORS标头都加倍?我正在使用Zuul代理,以通过API网关代理对服务的请求。

我的Spring安全性过滤顺序必须配置不正确。

当我访问需要身份验证的路由时,出现如下错误:

通过API网关请求服务错误

XMLHttpRequest cannot load https://myservice.mydomain.com:8095/service/v1/account/txHistory?acctId=0.  
The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. 
Origin 'http://localhost:9000' is therefore not allowed access.

Chrome网络日志

我检查了Chrome devtools中的响应,并确保足够重复CORS标头两次: 重复的CORS标头

因此,这看起来好像我的CORS过滤器针对每个回复都被调用了两次。我不知道为什么现在会发生这种情况。可能是在ChannelProcessingFilter之前添加了我的过滤器。

API网关CORS过滤器的代码:

public class SimpleCORSFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        res.setHeader("Access-Control-Max-Age", "3600");
        res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept, x-requested-with, Cache-Control");
        chain.doFilter(request, res);
    }

    @Override
    public void destroy() {}
}

我的API网关安全性配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Inject
    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService)
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
           .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .antMatchers("/health","/metrics", "/v1/users/register").permitAll()
                .antMatchers("/mappings", "/v1/**", "/service/**").authenticated()
                .and()
            .httpBasic()
                .realmName("apiRealm")
                .and()
            .csrf()
                .disable()
            .headers()
                .frameOptions().disable()
            .and().addFilterBefore(new SimpleCORSFilter(), ChannelProcessingFilter.class);
    }

}

我可以通过检查标头是否为空来解决此问题,然后仅在标头为空或null时进行设置,尽管这似乎不是最佳解决方案。我想了解我所做的使标头被预置两次的操作。

尼泊尔语

我有一个类似的问题,但问题是我在APIGateway和其他服务中都具有CORS筛选器。如果不是您的情况,请尝试使用此CORS过滤器。

将此添加到您在API网关中具有@EnableZuulProxy的类中。这应该可以解决问题,我在我的配置上也有类似的配置。

@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("HEAD");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("DELETE");
    config.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring Boot CORS过滤器-CORS预检通道未成功

来自分类Dev

使用Spring Boot 1.4无法获得CORS标头

来自分类Dev

为什么我的Spring Boot无状态过滤器被调用两次?

来自分类Dev

在Java Play应用中启用CORS过滤器

来自分类Dev

如何使用graphql-kotlin在Spring Boot服务器中添加CORS标头?

来自分类Dev

Spring Boot CSRF过滤器

来自分类Dev

在Spring Boot中为Servlet配置添加过滤器

来自分类Dev

如何在Spring Boot Web应用程序中配置2个单独的过滤器?

来自分类Dev

在Spring Boot WebMvcConfigurer中允许CORS的同时,请求的资源上没有“ Access-Control-Allow-Origin”标头

来自分类Dev

Tomcat CORS过滤器

来自分类Dev

Spring Boot过滤器模式匹配错误的URL

来自分类Dev

阻止Spring Boot注册Servlet过滤器

来自分类Dev

如何在Spring Boot中设置过滤器链?

来自分类Dev

Spring Boot CommandLineRunner:过滤器选项参数

来自分类Dev

Spring Boot-添加过滤器

来自分类Dev

Spring Boot过滤器模式匹配错误的URL

来自分类Dev

Spring Boot ApplicationPidListener的Java配置

来自分类Dev

Spring Boot ApplicationPidListener的Java配置

来自分类Dev

不带过滤器的Spring Boot API和带过滤器的Spring Boot API有什么区别?

来自分类Dev

Java + Spring Boot:我正在尝试将CacheControl标头添加到ResponseEntity

来自分类Dev

如何在Java Spring Boot中从请求标头获取承载令牌?

来自分类Dev

Spring Boot + Spring OAuth Java配置

来自分类Dev

Spring Boot + Spring OAuth Java配置

来自分类Dev

Java Spring Boot Restcontroller RequestMapping执行两次

来自分类Dev

如何在Spring Security中配置自定义身份验证过滤器-使用Java Config

来自分类Dev

java jersay多个ServletContainer过滤器

来自分类Dev

如何从Java过滤器调用Spring Controller

来自分类Dev

在Spring Boot Admin中为KubernetesDiscoveryClient指定Spring表达式语言过滤器

来自分类Dev

Flyway和MyBatis:Spring Boot的Java配置

Related 相关文章

  1. 1

    Spring Boot CORS过滤器-CORS预检通道未成功

  2. 2

    使用Spring Boot 1.4无法获得CORS标头

  3. 3

    为什么我的Spring Boot无状态过滤器被调用两次?

  4. 4

    在Java Play应用中启用CORS过滤器

  5. 5

    如何使用graphql-kotlin在Spring Boot服务器中添加CORS标头?

  6. 6

    Spring Boot CSRF过滤器

  7. 7

    在Spring Boot中为Servlet配置添加过滤器

  8. 8

    如何在Spring Boot Web应用程序中配置2个单独的过滤器?

  9. 9

    在Spring Boot WebMvcConfigurer中允许CORS的同时,请求的资源上没有“ Access-Control-Allow-Origin”标头

  10. 10

    Tomcat CORS过滤器

  11. 11

    Spring Boot过滤器模式匹配错误的URL

  12. 12

    阻止Spring Boot注册Servlet过滤器

  13. 13

    如何在Spring Boot中设置过滤器链?

  14. 14

    Spring Boot CommandLineRunner:过滤器选项参数

  15. 15

    Spring Boot-添加过滤器

  16. 16

    Spring Boot过滤器模式匹配错误的URL

  17. 17

    Spring Boot ApplicationPidListener的Java配置

  18. 18

    Spring Boot ApplicationPidListener的Java配置

  19. 19

    不带过滤器的Spring Boot API和带过滤器的Spring Boot API有什么区别?

  20. 20

    Java + Spring Boot:我正在尝试将CacheControl标头添加到ResponseEntity

  21. 21

    如何在Java Spring Boot中从请求标头获取承载令牌?

  22. 22

    Spring Boot + Spring OAuth Java配置

  23. 23

    Spring Boot + Spring OAuth Java配置

  24. 24

    Java Spring Boot Restcontroller RequestMapping执行两次

  25. 25

    如何在Spring Security中配置自定义身份验证过滤器-使用Java Config

  26. 26

    java jersay多个ServletContainer过滤器

  27. 27

    如何从Java过滤器调用Spring Controller

  28. 28

    在Spring Boot Admin中为KubernetesDiscoveryClient指定Spring表达式语言过滤器

  29. 29

    Flyway和MyBatis:Spring Boot的Java配置

热门标签

归档