SpringBoot ---- 跨域遇到的问题

it2023-06-20  69

Access to XMLHttpRequest at ‘http://localhost:88/api/sys/login’ from origin ‘http://localhost:8001’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

报错原因:没有进行跨域之间的相关设置 解决:

@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); } }

Access to XMLHttpRequest at ‘http://localhost:88/api/sys/login’ from origin ‘http://localhost:8001’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘http://localhost:8001, http://localhost:8001’, but only one is allowed.

报错原因:跨域规则设置了多个 解决:查看自己项目中的跨域设置,只用设置一次即可

最新回复(0)