当前位置: 首页 > article >正文

Spring Cloud Gateway 网关跨域问题解决

0、版本说明

Spring Cloud Version:Spring Cloud 2021.0.4
Spring Cloud Gateway Version:3.1.4
Spring Boot Version:2.6.11

1、网关跨域问题说明

  关于跨域的相关原理和理论,网上有大量文章对此进行说明,因此博主在这里就不再赘述,这里仅说明对于在同一注册中心中注册的服务,网关可以通过在注册中心注册的服务名对相应请求找到对应的服务进行路由转发,因此这种情况,不存在跨域问题,但是对于一些通过Nginx反向代理到网关服务下的请求进行访问时,就存在了跨域问题,所以下面网关配置也是针对此部分问题进行解决。

2、网关跨域解决

针对网关跨域解决,这里提供两种解决方案,仅供参考,下面配置均在线上环境测试通过,关于其他版本,仅供参考!

2.1、方案一:网关注入配置类

Spring Cloud Gateway提供了跨域的配置类,然后在网关项目代码中添加一个CorsWebFilter类即可实现,关于网关提供的Cors配置类,可参看官方文档(CorsConfiguration (Spring Framework 5.0.20.RELEASE API))

@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 这里仅为了说明问题,配置为放行所有域名,生产环境请对此进行修改
        config.addAllowedOrigin("*");
        // 放行的请求头
        config.addAllowedHeader("*");
        // 放行的请求方式,主要有:GET, POST, PUT, DELETE, OPTIONS
        config.addAllowedMethod("*"); 
        // 暴露头部信息
        config.addExposedHeader("*"); 
        // 是否发送cookie
        config.setAllowCredentials(true); 
        
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

说明:

        由于spring-framework从5.3.0版本开始,关于CORS跨域配置类 CorsConfiguration 中将 addAllowedOrigin 方法名修改为 addAllowedOriginPattern(spring-framework项目对应的类信息:https://github.com/spring-projects/spring-framework/blob/v5.3.0/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java),所以,如果项目中 spring-framework 版本高于5.3.0,请使用如下配置类代码。

@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 这里仅为了说明问题,配置为放行所有域名,生产环境请对此进行修改
        config.addAllowedOriginPattern("*");
        // 放行的请求头
        config.addAllowedHeader("*");
        // 放行的请求方式,主要有:GET, POST, PUT, DELETE, OPTIONS
        config.addAllowedMethod("*"); 
        // 暴露头部信息
        config.addExposedHeader("*"); 
        // 是否发送cookie
        config.setAllowCredentials(true); 
        
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

2.2、方案二:网关yaml文件添加配置

Spring Cloud Gateway 也提供了可以直接通过在yaml文件中配置的方式解决跨域问题,具体的类配置可以查看源码中对应的类org.springframework.cloud.gateway.config.GlobalCorsProperties,源码地址如下:

https://github.com/spring-cloud/spring-cloud-gateway/blob/v3.1.4/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GlobalCorsProperties.java

网关yaml配置如下:

spring:
  cloud:
    gateway:
      # 网关全局跨域配置
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "*"
            allowedMethods: "*"
            allowedHeaders: "*"
            allowCredentials: true
        # 解决options请求被拦截的问题
        add-to-simple-url-handler-mapping: true

说明:

        由于spring-framework从5.3.0版本开始,关于CORS跨域配置类 CorsConfiguration 中将 allowedOrigins 变量名修改为 allowedOriginPatterns(spring-framework项目对应的类信息)所以,如果项目中 spring-framework 版本高于5.3.0,请使用如下配置代码。

spring:
  cloud:
    gateway:
      # 网关全局跨域配置
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOriginPatterns: "*"
            allowedMethods: "*"
            allowedHeaders: "*"
            allowCredentials: true
        # 解决options请求被拦截的问题
        add-to-simple-url-handler-mapping: true


http://www.kler.cn/a/144738.html

相关文章:

  • DBeaver连接Oracle时报错:Undefined Error
  • acwing算法基础之数学知识--Nim游戏和集合Nim游戏
  • echarts的横向柱状图文字省略,鼠标移入显示内容 vue3
  • 【机器学习】聚类(一):原型聚类:K-means聚类
  • labelImg
  • Docker的项目资源参考
  • 2023年亚太杯数学建模思路 - 案例:粒子群算法
  • css 常用动画效果
  • Selenium介绍及基本使用方法
  • 【ArcGIS Pro微课1000例】0037:ArcGIS Pro中模型构建器的使用---以shp批量转kml/kmz为例
  • RabbitMq使用与整合
  • linux上的通用拍照程序
  • 从0到0.01入门 Webpack| 004.精选 Webpack面试题
  • (4)BUUCTF-web-[极客大挑战 2019]EasySQL1
  • 淘宝商品详情接口,商品属性接口,商品信息查询,商品详细信息接口,h5详情,淘宝APP详情
  • ⑦【Redis GEO 】Redis常用数据类型:GEO [使用手册]
  • 基于51单片机的公交自动报站系统
  • Oracle 最终抛弃了 Sun !
  • Java枚举
  • ARM - AArch64 - 通用寄存器