如何从环境中设置Spring Cloud Gatweay的CORS允许原点的值?

mnemlml8  于 2022-10-30  发布在  Spring
关注(0)|答案(1)|浏览(165)

How do I get the value of CORS allowed origin from spring cloud gateway互补
哪个环境变量将Map到以下点?

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "*" # << how do I change this?
6rqinv9w

6rqinv9w1#

根据操作系统的不同,可能不允许使用该密钥,但可以选择使用SPRING_APPLICATION_JSON。请检查外部化配置。
假设您具有以下属性

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "*"
            allowedHeaders: "*"
            allowedMethods:
            - GET
            - POST

将此yaml转换为json。对于eg

{
  "spring": {
    "cloud": {
      "gateway": {
        "globalcors": {
          "corsConfigurations": {
            "[/**]": {
              "allowedOrigins": "*",
              "allowedHeaders": "*",
              "allowedMethods": [
                "GET",
                "POST"
              ]
            }
          }
        }
      }
    }
  }
}

修剪eg using的白色

{"spring":{"cloud":{"gateway":{"globalcors":{"corsConfigurations":{"[/**]":{"allowedOrigins":"*","allowedHeaders":"*","allowedMethods":["GET","POST"]}}}}}}}

现在,在env变量中,您可以设置

SPRING_APPLICATION_JSON = '{"spring":{"cloud":{"gateway":{"globalcors":{"corsConfigurations":{"[/**]":{"allowedOrigins":"*","allowedHeaders":"*","allowedMethods":["GET","POST"]}}}}}}}'

您可以编写某种CLI脚本来执行此操作。

相关问题