通过@Configuration注解自定义config类继承WebMvcConfigurerAdapter类实现
@Configuration
public class CORSConfig extends WebMvcConfigurerAdapter {
@Value("${xxxx}")
private String[] allowedOrigin;
/**
* logger
*/
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
for (int i = 0; i < allowedOrigin.length; i++) {
config.addAllowedOrigin(allowedOrigin[i]);
}
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}