【问题标题】:Netflix Zuul CORSNetflix Zuul CORS
【发布时间】:2016-06-15 05:31:32
【问题描述】:

您好所有 spring boot/cloud/Netflix Zuul 专家!

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

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

我已经使用了 Zuul 过滤器(pre、route 和 post)来尝试将适当的访问控制标头添加到响应中,当我从 Rest 客户端提交 POST 请求时,我可以看到这是有效的(我正在使用 Paws)但是当通过在浏览器中运行的 Angular JavaScript 提交请求时,过滤器未处理 CORS 预检 OPTIONS 请求,实际上 Zuul 返回 403 错误,浏览器当然会报告 CORS 错误。

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

【问题讨论】:

  • 您遇到的错误是什么?我也从 AngularJS 中遇到问题,CORS 标头加倍,我认为这是因为我的 API 网关(Zuul 代理)和资源服务器(服务)中有一个 CORS 过滤器。
  • 我没有收到从 Zuul 返回的任何 CORS 标头。我暂时禁用了 Zuul,直到我弄清楚这一点。我怀疑我会尽量不要太聪明,让资源服务器处理 CORS 处理和 Zuul 代理一切。当我有更新时,我会更新这篇文章。原则上,我仍然认为让 Zuul 处理 CORS 和身份验证是个好主意。

标签: spring-boot cors netflix-zuul


【解决方案1】:

我在使用自定义 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;
}

希望对你有帮助!

【讨论】:

  • 在哪里添加这些 bean。请提供详细信息。我很长一段时间都陷入了这些问题
【解决方案2】:

我遇到了这个问题,所以在这里放一个解决方案...基本上,需要添加 WebMvcConfigurer 的实现。 Springboot版本:2.3.2.RELEASE,Cloud版本:Hoxton.SR6...更多详情请参考this link

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class CorsConfig implements WebMvcConfigurer {

@Value("#{'${allowedOriginList}'.split(',')}") 
private String[]allowedOriginList;

@Override
public void addCorsMappings(CorsRegistry registry) {
    log.info("adding cors config");
    registry.addMapping("/**").allowedOrigins(allowedOriginsList).allowedMethods("*").allowedHeaders( "authorization").allowCredentials(true).exposedHeaders("Cache-Control", "Content-Type");

 }  
}

然后在你的 application.yml 中定义属性 allowedOriginList

allowedOriginList:  http://localhost:4200

【讨论】:

  • 我更喜欢这个答案,但你忘了一点:.allowedOrigins("*") 调用应该使用定义的属性,因此:.allowedOrigins(allowOriginList)。我提出了修改建议。
【解决方案3】:

您应该在主类中添加 CorsFilter。在大多数情况下,它应该可以工作。但如果你仍然得到预检 CORS,试试这个。

创建一个名为 ZuulSecurity 的新类。

@Configuration
@EnableAutoConfiguration
public class ZuulSecurity extends WebSecurityConfigurerAdapter {
  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().antMatcher("/**")
            .authorizeRequests()
            .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
            .anyRequest().permitAll();
  }
}

您应该知道 Zuul Proxy 需要身份验证。在开发中,添加上面的代码块将使您的 API Gateway 允许预检请求并删除 Zuul 的身份验证过程。

【讨论】:

    猜你喜欢
    • 2017-04-19
    • 2017-08-07
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 2015-11-11
    • 2020-04-15
    • 2016-09-11
    • 2017-06-08
    相关资源
    最近更新 更多