在當前前後端分離技術熱火的情況下,在開發過程中難免會遇到跨域問題,記錄一次跨域配置的代碼:
@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);
}
}