gpt4 book ai didi

spring-security - Zuul Proxy CORS header 包含多个值, header 重复两次-Java Spring Boot CORS过滤器配置

转载 作者:行者123 更新时间:2023-12-04 13:13:09 31 4
gpt4 key购买 nike

为什么我要让每个CORS header 都加倍?我正在使用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 header 重复两次: CORS Headers repeated

因此,这看起来好像我的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);
}

}

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

最佳答案

我有一个类似的问题,但问题是我在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);
}

关于spring-security - Zuul Proxy CORS header 包含多个值, header 重复两次-Java Spring Boot CORS过滤器配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36042862/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com