@Configuration
public class CorsConfig {
/**
* 跨域配置
* @return
*/
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
corsConfiguration.setMaxAge(18000L);
// 允许cookies跨域
corsConfiguration.setAllowCredentials(true);
// 允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
corsConfiguration.addAllowedOrigin("*");
// 允许访问的头信息,*表示全部
corsConfiguration.addAllowedHeader("*");
// 允许提交请求的方法类型,*表示全部允许
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
/**
* 自定义跨域过滤器
* @return
*/
@Bean
public CorsWebFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", buildConfig());
return new CorsWebFilter(source);
}
}
解决网关gateway跨域问题
最新推荐文章于 2024-01-17 16:53:42 发布