Spring security Cors配置不';Minikube上的spring webflux不起作用
我有下面的spring安全配置和一个调用后端服务的reactJs应用程序, 当我在minikube外运行ReactJs应用程序时效果很好,但当我在minikube上运行它时,我得到了 跨源请求被阻止:同源策略不允许读取远程资源 Spring安全配置Spring security Cors配置不';Minikube上的spring webflux不起作用,spring-security,spring-webflux,minikube,Spring Security,Spring Webflux,Minikube,我有下面的spring安全配置和一个调用后端服务的reactJs应用程序, 当我在minikube外运行ReactJs应用程序时效果很好,但当我在minikube上运行它时,我得到了 跨源请求被阻止:同源策略不允许读取远程资源 Spring安全配置 @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { return http .cors()
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
return http
.cors()
.configurationSource(configurationSource())
.and()
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint((exchange, e) ->
Mono.fromRunnable(() -> exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED)))
.accessDeniedHandler((exchange, e) ->
Mono.fromRunnable(() -> exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN)))
.and()
.securityContextRepository(securityContextRepository)
.authenticationManager(authenticationManager)
.authorizeExchange()
.pathMatchers(HttpMethod.OPTIONS).permitAll()
.pathMatchers(HttpMethod.POST, "/auth/signin").permitAll()
.pathMatchers(HttpMethod.POST, "/auth/users").permitAll()
.anyExchange().authenticated()
.and().build();
}
private CorsConfigurationSource configurationSource() {
var config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
return serverWebExchange -> config;
}
响应的HTTP状态代码是什么?您可以使用浏览器devtools中的网络窗格进行检查。是4xx还是5xx错误,而不是200 OK成功响应?没有响应我认为它在进入服务器之前就被阻止了,也许minikube会阻止它?我尝试了这个方法,但没有成功——额外配置'apiserver.cors allowed origins=[“http://*”]'-v 10 startYeah如果没有响应,则不是cors问题。CORS从不阻止浏览器接收响应。我知道问题出在哪里,前端是通过ClusterIP服务调用后端的,由于调用来自浏览器,服务无法访问,因此将其更改为NodePort,它就工作了。这是一个误解,我认为因为两个服务都在同一个集群上,ClusterIP应该可以,但实际上调用来自浏览器