Adam K
Adam K

Reputation: 21

'Invalid CORS request' returned by microservice after passing Zuul gateway

I have a Spring Boot service behind a Zuul gateway, deployed on a remote server. I am accessing these through an Ionic app from my local machine. I've setup a corsFilter in my Zuul application as follows:

@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new org.springframework.web.filter.CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return bean;
}

The request is accepted by Zuul and passed on to the microservice (I can see it in the microservice logs) however the microservice rejects the request and returns a 403 error 'Invalid CORS request'.

Any ideas?

Upvotes: 2

Views: 1811

Answers (1)

pan
pan

Reputation: 1967

You can also try to use the spring cors filter.

@Bean
public CorsFilter corsFilter() {
  final UrlBasedCorsConfigurationSource source = new 
  UrlBasedCorsConfigurationSource();
  final CorsConfiguration config = new CorsConfiguration();
  config.setAllowCredentials(true);
  config.addAllowedOrigin("*");
  config.addExposedHeader("Content-Type");
  config.addExposedHeader("Authorization");
  config.addExposedHeader("Accept");
  config.addExposedHeader("Origin");
  config.addAllowedHeader("*");
  config.addAllowedMethod("*");
  source.registerCorsConfiguration("/**", config);
  return new CorsFilter(source);
} 

Upvotes: 1

Related Questions