前后分离项目,在使用zuul 作微服务的网关的时候。需要设置跨域问题。
除了设置CorsFilter 还需要额外忽略重复头文件的配置。
zuul.ignored-headers: Access-Control-Allow-Credentials, Access-Control-Allow-Origin
具体步骤:
@Bean
@Order
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
//允许跨域
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
zuul:
ignored-headers: Access-Control-Allow-Credentials, Access-Control-Allow-Origin