14. CORS 配置

您可以配置网关以控制 CORS 行为。“全局” CORS 配置是 URL 模式到 Spring Framework CorsConfiguration 的映射。 以下是配置 CORS 的示例:spring-doc.cadn.net.cn

示例 69. application.yml
spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

在前面的例子中,允许来自源为docs.spring.io的所有GET请求路径的CORS请求。spring-doc.cadn.net.cn

要对不受某些网关路由谓词处理的请求应用相同的CORS配置,请将spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping属性设置为1。当路由谓词因HTTP方法为options而评估为true时,这很有用,以便支持CORS预检请求。spring-doc.cadn.net.cn