当前位置 博文首页 > 超负荷小生的博客:SpringBoot ---- 跨域遇到的问题
报错原因:没有进行跨域之间的相关设置
解决:
@Configuration
public class selectionCorsConfig {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
//配置跨域
corsConfiguration.addAllowedHeader("*"); //允许所有的头部进行跨域
corsConfiguration.addAllowedMethod("*"); //允许所有的请求方式进行跨域
corsConfiguration.addAllowedOrigin("*"); //允许所有来源进行访问跨域
corsConfiguration.setAllowCredentials(true); //允许携带cookies
source.registerCorsConfiguration("/**",corsConfiguration); //任意路径都进行跨域配置
return new CorsWebFilter(source);
}
}
报错原因:跨域规则设置了多个
解决:查看自己项目中的跨域设置,只用设置一次即可