问题描述
在解决跨域问题时出现The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*’, but only one is allowed.错误
原因是设置了相同的跨域信息。查看响应头如下:
解决方法
就是去除掉多次的跨域配置,只保留一次。比如我在网关配置了一个跨域,然后使用renren-fast管理员项目时里面的config又配置有跨域,所以我们需要把管理员项目里的跨域配置注释掉即可。
跨域配置
@Configuration
public class MyCorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 1、配置跨域
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
// 2、设置哪些路径需要跨域
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
}
}