Netflix Zuul CORS [英] Netflix Zuul CORS

查看:106
本文介绍了Netflix Zuul CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,所有春季启动人员/云/Netflix Zuul专家!

我正在运行一个使用Netflix OSS组件的微服务环境,以及一个使用eureka和Zuul进行服务发现和路由的Spring Boot.多个微服务通过docker部署在多个VPS上.我正在运行一个Angular JS客户端,该客户端使用Zuul路由通过单个端点访问这些微服务.

我正在使用yeoman hottowel进行快速发展的角度支架,但是由于Web服务器在localhost:3000上运行并且试图通过Zuul路由器运行来调用RESTful端点,因此我遇到了CORS问题别处.

我一直在使用Zuul过滤器(前置,路由和后置)尝试将适当的访问控制标头添加到响应中,并且当我从Rest客户端提交POST请求时,可以看到这可行(我正在使用Paws),但是当通过浏览器中运行的角度JavaScript提交请求时,过滤器未处理CORS预检OPTIONS请求,实际上Zuul返回403错误,并且浏览器当然会报告CORS错误. /p>

也许在生产中,我可以从Zuul端点提供JavaScript,而不会遇到这个问题,但是我想知道是否有一种方法可以在Zuul中配置所有的CORS处理?

解决方案

自定义cors过滤器存在相同的问题.我通过在zuul项目中添加以下代码来解决了这个问题:

 @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;
}

我希望这会对您有所帮助!

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?

解决方案

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!

这篇关于Netflix Zuul CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆