在当前前后端分离技术热火的情况下,在开发过程中难免会遇到跨域问题,记录一次跨域配置的代码:
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource(new PathPatternParser());
CorsConfiguration corsConfiguration=new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedMethod("*");// 允许提交请求的方法类型,*表示全部允许
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
}
}