创建CrossDomainConfig类:
@Configuration public class CrossDomainConfig{ // 当前跨域请求最大有效时长 private final long MAX_AGE = 12*60*60*1000; @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("http://localhost:8080"); // 添加访问地址这里的Http://必须加上 corsConfiguration.addAllowedHeader("*"); // 设置访问源请求头 corsConfiguration.addAllowedMethod("*"); // 设置请求方式 corsConfiguration.setAllowCredentials(false); //是否发送 Cookie corsConfiguration.setMaxAge(MAX_AGE); source.registerCorsConfiguration("/**", corsConfiguration); // 对接口配置跨域设置 return new CorsFilter(source); } }