问题
Hello all spring boot / cloud / Netflix Zuul experts !
I am running a microservice environment using the Netflix OSS components and Spring Boot using eureka and Zuul for service discovery and routing. Multiple micro services are deployed on multiple VPS via docker. I am running an Angular JS client which accesses these microservices via a single endpoint using Zuul routing.
I am using yeoman hottowel for the angular scaffolding for the rapid development that this enables, however I am hitting a problem with CORS since the web server is running on localhost:3000 and trying to invoke a RESTful endpoint through the Zuul router running elsewhere.
I have played around with the Zuul filters (pre,route and post) to try and add the appropriate access-control headers to the response, and I can see that this works when I submit a POST request from a Rest client (I am using Paws) but when the request is submitted via angular JavaScript running in a browser, the CORS preflight OPTIONS request is not being handled by the filter, in fact Zuul returns a 403 error and the browser of course reports a CORS error.
Maybe in production I can serve the JavaScript from the Zuul endpoint and not face this problem, but I would like to know if there is a way of configuring all the CORS handling within Zuul?
回答1:
I had the same problem with custom cors filters. I solved this by adding the code in the zuul project as following:
@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(0);
return bean;
}
I hope this will help you!
来源:https://stackoverflow.com/questions/35745938/netflix-zuul-cors