Thursday, August 12, 2021

Spring Cloud - Zuul Proxy is producing a No 'Access-Control-Allow-Origin' ajax response | cors

The problem was NOT in Zuul, but in Spring Security.

Goto class SecurityConfig and add this code

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
http.cors();
    }

@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin(CorsConfiguration.ALL);
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 source;
}

}

Then goto yml and add this code:

zuul: 
    ignored-headers: Access-Control-Allow-Credentials, Access-Control-Allow-Origin

No comments:

Post a Comment