Spring Cloud Gateway 网关跨域问题解决 |
您所在的位置:网站首页 › 网关无效怎么解决 › 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配置类,可参看官方文档(https://docs.spring.io/spring-framework/docs/5.0.x/javadoc-api/org/springframework/web/cors/CorsConfiguration.html) @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说明: 1> 由于spring-framework从5.3.0版本开始,关于CORS跨域配置类 CorsConfiguration 中将 allowedOrigins 变量名修改为 allowedOriginPatterns(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,请使用如下配置代码。 spring: cloud: gateway: # 网关全局跨域配置 globalcors: cors-configurations: '[/**]': allowedOriginPatterns: "*" allowedMethods: "*" allowedHeaders: "*" allowCredentials: true # 解决options请求被拦截的问题 add-to-simple-url-handler-mapping: true2> 针对此解决方案,官方文档有对应的配置说明,具体可参看下图,下图中红线框出部分需要在配置时注意,记得添加上add-to-simple-url-handler-mapping配置,官方文档地址如下:https://docs.spring.io/spring-cloud-gateway/docs/3.1.4/reference/html/#cors-configuration |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |