springboot 跨域解决方案
第一种全局处理:
@Configuration public class CorsConfig implements WebMvcConfigurer { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); // 1允许任何域名使用 corsConfiguration.addAllowedOrigin("*"); // 2允许任何头 corsConfiguration.addAllowedHeader("*"); // 3.允许ajax异步请求带cookie信息 corsConfiguration.setAllowCredentials(true); // 4允许任何方法(post、get等) corsConfiguration.addAllowedMethod("*"); return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); return new CorsFilter(source); } }
第二种:加在方法上面:
@CrossOrigin(origins = CorsConfiguration.ALL)