spring-boot - Netflix Zuul CORS

标签 spring-boot cors netflix-zuul

各位 Spring Boot/云/Netflix Zuul 专家大家好!

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

我正在使用 yeoman hottowel 作为角度脚手架,以实现快速开发,但是我遇到了 CORS 问题,因为 Web 服务器在 localhost:3000 上运行,并尝试通过运行的 Zuul 路由器调用 RESTful 端点别处。

我已经尝试使用 Zuul 过滤器(前置、路由和后置)来尝试将适当的访问控制 header 添加到响应中,并且当我从 Rest 客户端提交 POST 请求时,我可以看到这是有效的(我正在使用 Paws),但是当通过在浏览器中运行的 Angular JavaScript 提交请求时,过滤器不会处理 CORS 预检选项请求,事实上 Zuul 返回 403 错误,浏览器当然会报告 CORS 错误。

也许在生产中我可以从 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;
}

希望这对您有帮助!

关于spring-boot - Netflix Zuul CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745938/

相关文章:

unit-testing - Zuul 没有抛出预期的异常

java - 如何将 NTLM 凭据传递给 zuul 请求 header

spring - 自定义 Spring Zuul 动态路径

javascript - 为什么我的 JavaScript 代码收到“"No ' Access-Control-Allow-Origin' header is present on the requested resources”错误,而 Postman 却没有?

google-maps-api-3 - 谷歌地图 - 跨域请求被阻止 : The Same Origin Policy disallows reading the remote resource at

spring-boot - 如何在 Spring Boot 中设置 Hibernate 方言?

java - 未能关闭 Spring Boot 安全配置

jquery - 重定向后 HTML5 CORS 请求在 Safari 中失败

spring-boot - 将 Camel Spring XML 迁移到 Spring Boot

java - Elasticsearch java 客户端初始化失败