gateway跨域问题

在使用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("*"); 去掉之后问题解决。

相关推荐
过期动态1 分钟前
Java开发中的@EnableWebMvc注解和WebMvcConfigurer接口
java·开发语言·spring boot·spring·tomcat·maven·idea
摇滚侠2 分钟前
IDEA 定义返回值快捷键
java·ide·intellij-idea
毕设源码-郭学长4 分钟前
【开题答辩全过程】以 高校考勤管理系统为例,包含答辩的问题和答案
java·eclipse
A懿轩A43 分钟前
【Maven 构建工具】从零到上手 Maven:安装配置 + IDEA 集成 + 第一个项目(保姆级教程)
java·maven·intellij-idea
野犬寒鸦1 小时前
从零起步学习并发编程 || 第一章:初步认识进程与线程
java·服务器·后端·学习
我爱娃哈哈1 小时前
SpringBoot + Flowable + 自定义节点:可视化工作流引擎,支持请假、报销、审批全场景
java·spring boot·后端
XiaoFan0121 小时前
将有向工作流图转为结构树的实现
java·数据结构·决策树
小突突突2 小时前
浅谈Java中的反射
java·开发语言
Anastasiozzzz2 小时前
LeetCode Hot100 295. 数据流的中位数 MedianFinder
java·服务器·前端
我真的是大笨蛋2 小时前
Redo Log详解
java·数据库·sql·mysql·性能优化