找来找去就这个生效了
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;@Configurationpublic class CorsConfig {/*** 跨域支持* @author Alaia*/@Beanpublic CorsFilter corsFilter() {final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();final CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true); // 允许cookies跨域config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许config.addAllowedHeader("*");// #允许访问的头信息,*表示全部config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许source.registerCorsConfiguration("/**", config);return new CorsFilter(source);}}
