在使用gateway做网关过程中配置跨域配置:
java
/**
* 跨域请求配置
*/
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 允许的请求头
corsConfiguration.addAllowedHeader("*");
// 允许的请求源 (如:http://localhost:8080)
corsConfiguration.addAllowedOrigin("*");
// 允许的请求方法 ==> GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
corsConfiguration.addAllowedMethod("*");
//允许的源
corsConfiguration.addAllowedOriginPattern("*");
// URL 映射 (如: /admin/**)
UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(urlBasedCorsConfigurationSource);
}
}
运行之后在控制台发现错误: xxx has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the request resouce.
需要去掉:
corsConfiguration.addAllowedOriginPattern("*"); 去掉之后问题解决。