lixinglin
lixinglin

Reputation: 71

How to remove the response header, such as Access-Control-Allow-Origin, Access-Control-Allow-Credentials

I know that the zuul gateway can be removed by configuration, but how is springcloud-gateway implemented?

zuul: sensitive-headers: Cookie,Set-Cookie ignored-headers: Access-Control-Allow-Origin,Access-Control-Allow-Credentials

Upvotes: 3

Views: 5765

Answers (1)

dlsrb6342
dlsrb6342

Reputation: 368

It can be done with RemoveRequestHeaderGatewayFilterFactory or RemoveResponseHeaderGatewayFilterFactory or RemoveHopByHopHeadersFilter.

Please check spring-cloud-gateway guide. Choose a filter that fits your use case.

you can configure like below.

# RemoveHopByHopHeadersFilter
spring.cloud.gateway.filter.remove-hop-by-hop: 
  - Access-Control-Allow-Origin
  - Access-Control-Allow-Credentials

# RemoveResponseHeaderGatewayFilterFactory
spring:
  cloud:
    gateway:
      routes:
        - id: test-route
          uri: http://test.org
          filters:
            - RemoveResponseHeader= Access-Control-Allow-Origin

RemoveHopByHopHeadersFilter is applied to all routes by default. But If you want to apply RemoveRequestHeaderGatewayFilterFactory or RemoveResponseHeaderGatewayFilterFactory to all routes, you have to set spring.cloud.gateway.default-filters property.

Upvotes: 4

Related Questions