在使用gateway做网关过程中配置跨域配置:
/**
* 跨域请求配置
*/
@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.
需要去掉:文章来源:https://www.toymoban.com/news/detail-637851.html
corsConfiguration.addAllowedOriginPattern("*"); 去掉之后问题解决。文章来源地址https://www.toymoban.com/news/detail-637851.html
到了这里,关于gateway跨域问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!