Use Spring Gateway and getting error: 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed

Viewed 1169

I am using Spring Gateway framework and configured as follows.

 gateway:
  default-filters:
    - DedupeResponseHeader=Access-Control-Allow-Origin
  globalcors:
    cors-configurations:
      '[/**]':
        allowedOrigins: "*"
        allowedMethods: "*"
        allowedHeaders: "*"

But when I try to execute a request on the gateway, I get the following error.

has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.

I do not understand how to configure the configuration to avoid this error

3 Answers

Try with this configuration:

spring:
  cloud:
    gateway:
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials, RETAIN_UNIQUE
      globalcors:
          cors-configurations:
             '[/**]':
             allowed-origins: "*"
             allowed-methods: "*"
             allowed-headers: "*"
             allow-credentials: true

In addition to Pablo's answer, make sure downstream services do not have cors configured because they will add their headers to the final response headers. Therefore you only configure cors for your gateway service and you can just close the ports to other services so they can only be accessed from within the server.

Related