Queria saber como devo configurar o cors quando uso gateway. Neste momento tenho o seguinte codigo:
Gateway:
@Configuration
public class CorsConfiguration extends org.springframework.web.cors.CorsConfiguration {
@Bean
public CorsWebFilter corsFilter() {
org.springframework.web.cors.CorsConfiguration corsConfiguration = new org.springframework.web.cors.CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("http://localhost:3000");
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"));
corsConfiguration.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(source);
}
}
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
@Configuration
public class SecurityConfig {
@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http, ServerProperties serverProperties,
@Value("origins") String[] origins, @Value("permit-all") String[] permitAll) throws Exception {
http.oauth2ResourceServer(resourceServer -> {
resourceServer.jwt(jwt -> jwt.jwtAuthenticationConverter(new JwtAuthenticationConverterImpl()));
});
// State-less session (state in access token only)
http.securityContextRepository(NoOpServerSecurityContextRepository.getInstance());
// Disable CSRF because of state-less session-management
http.csrf(csrf -> csrf.disable());
// Return 401 (unauthorized) instead of 302 (redirect to login) when
// authorization is missing or invalid
http.exceptionHandling(exceptionHandling -> {
exceptionHandling.accessDeniedHandler(accessDeniedHandler());
});
// If SSL enabled, disable http (https only)
if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) {
http.redirectToHttps();
}
http.authorizeExchange(exchange -> exchange.pathMatchers(permitAll).permitAll().anyExchange().permitAll());
return http.build();
}
}
Outro micro-serviço:
@Configuration
public class WebConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*");
}
};
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
private SecurityFilter securityFilter;
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf(csrf -> csrf.disable())
.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(req -> {
req.anyRequest().permitAll();
})
// .addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
}
E ao fazer uma requisição que retorna 200 OK recebo o seguinte erro:
Alguem me consegue ajudar?