SpringBoot 跨域
看大部分文章都是通过WebMvcConfigurer 来处理的, 这样的话会导致其他配置紊乱
发一下我常用的配置
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;
/**
* 跨域配置
*
* @author ly-chn
*/
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin(CorsConfiguration.ALL);
corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
// 允许cookie, 我没这么用过
// corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
}
好处是不会影响其他配置, 2.x和3.x通用
标签:SpringBoot,corsConfiguration,解决方案,org,springframework,CorsConfiguration,import,跨 From: https://www.cnblogs.com/liangyun/p/17286004.html