0

Complements How do I get the value of CORS allowed origin from spring cloud gateway

What environment variable would map to the following point?

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "*" # << how do I change this?
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

1 Answers1

1

Depending upon OS, the key might not be allowed, but the one of the option would be to use SPRING_APPLICATION_JSON. Check externalize configuration.

Lets say you have following properties

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

Convert this yaml to json. For eg

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

Trim any white spaces for eg using

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

Now in your env variable you can set

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

You can write some kind of CLI script to do this.

Dhananjay
  • 1,140
  • 1
  • 12
  • 28