SpringCloudGetaway跨域
在yml中添加
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
# 允许携带认证信息
allow-credentials: true
# 允许跨域的源(网站域名/ip),设置*为全部
allowedOrigins: "*"
# 允许跨域的method, 默认为GET和OPTIONS,设置*为全部
allowedMethods: "*"
# 允许跨域请求里的head字段,设置*为全部
allowedHeaders: "*"
如上如果还是会出现跨域报错的话需要在加上一个配置
spring:
cloud:
gateway:
default-filters:
- DedupeResponseHeader=Access-Control-Allow-Origin, RETAIN_UNIQUE
解释:报错原因为网关所调用的微服务本身也具有防跨域, gateway 和下游系统同时支持了跨域,都会向 response header
中增加 Access-Control-Allow-Origin
*,所以出现重复的 *。所以需要使用DedupeResponseHeader
来处理一下响应头重复的问题。
DedupeResponseHeader
有三个策略:RETAIN_FIRST
, RETAIN_LAST
, RETAIN_UNIQUE
,我们使用RETAIN_UNIQUE
即可。
注意: 该版本需要使用高版本的getaway,推荐使用G版以上
如果觉得有帮助的话给个免费的点赞吧,Thanks♪(・ω・)ノ