首页 > 代码库 > Spring Boot跨域问题解决方案

Spring Boot跨域问题解决方案

@Configuration
public class CorsConfig {

@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE); //一定要最高优先级才行,集成OAuth2的时候需要
return bean;
}
}

Spring Boot跨域问题解决方案