大家好,所有的spring boot / cloud / Netflix Zuul专家!
我正在运行一个微服务环境,使用Netflix OSS组件和Spring Boot,使用eureka和Zuul进行服务发现和路由。通过docker在多个VPS上部署多个微服务。我正在运行一个Angular JS客户端,它使用Zuul路由通过单个端点访问这些微服务。
我使用yeoman热毛巾作为快速开发的脚手架,但是我遇到了CORS的问题,因为web服务器在localhost:3000上运行,并试图通过在其他地方运行的Zuul路由器调用RESTful端点。
我尝试过使用Zuul过滤器(pre,route和post)来尝试向响应中添加适当的访问控制头,我可以看到当我从Rest客户端(我使用Paws)提交POST请求时,这是有效的,但是当请求是通过在浏览器中运行的angular JavaScript提交时,CORS印前检查选项请求没有被过滤器处理,事实上Zuul返回403错误,浏览器当然会报告CORS错误。
也许在生产中,我可以从Zuul端点为JavaScript服务,而不会面临这个问题,但我想知道是否有一种方法可以在Zuul中配置所有CORS处理?
发布于 2017-09-18 18:35:14
我在自定义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;
}我希望这能对你有所帮助!
发布于 2020-11-16 08:30:28
我遇到了这个问题,所以我提出了一个解决方案here...Basically,需要添加一个WebMvcConfigurer的实现。Springboot版本: 2.3.2.RELEASE,云版本: 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发布于 2021-07-03 12:39:36
您应该在主类中添加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代理需要身份验证。在开发中,添加上面的代码块将允许您的API Gateway进行印前检查请求,并删除Zuul的身份验证过程。
https://stackoverflow.com/questions/35745938
复制相似问题