摘要:无。
环境
Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
Spring Cloud 2021.0.5
Spring Cloud Gateway 3.1.4
1 跨域策略
通过配置文件声明跨域策略:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| spring: cloud: gateway: globalcors: add-to-simple-url-handler-mapping: true cors-configurations: '[/**]': allow-credentials: true allowed-origins: - https://www.a.com - https://www.b.com allowed-methods: - GET - POST - PUT - DELETE - OPTIONS allowed-headers: "*"
|
2 响应头去重
如果网关和下游服务都配置了跨域,响应中会出现重复的响应头,导致浏览器报错,此时应遵循跨域只在网关层配置一次的原则,关闭下游服务的跨域配置。
如果下游无法关闭,就需要在网关上对跨域响应头去重:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| spring: cloud: gateway: routes: - id: cors-route uri: http://127.0.0.1:8080 predicates: - Path=/demo/** filters: - name: DedupeResponseHeader args: name: - Access-Control-Allow-Origin - Access-Control-Allow-Credentials strategy: RETAIN_FIRST
|
条