目前若依文档上提供的跨域解决方案是修改gateway-yml文件:

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOriginPatterns: "*"
            allowed-methods: "*"
            allowed-headers: "*"
            allow-credentials: true
            exposedHeaders: "Content-Disposition,Content-Type,Cache-Control"

实际上述配置中少了一个属性是

allowedOrigins: "*"

这个是允许所有IP跨域访问的配置
所以正确配置如下:

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
          	allowedOrigins: "*" #允许所有ip跨域访问
            allowedOriginPatterns: "*"
            allowed-methods: "*" #允许所有请求方式
            allowed-headers: "*" #允许任何头进行跨域
            allow-credentials: true #允许携带cookie
            exposedHeaders: "Content-Disposition,Content-Type,Cache-Control"

网上很多解决方案也是瞎抄,看来看去也解决不了问题

Logo

快速构建 Web 应用程序

更多推荐