DedupeResponseHeader 過濾器

DedupeResponseHeader GatewayFilter 工廠接受一個 name 引數和一個可選的 strategy 引數。name 可以包含一個以空格分隔的頭部名稱列表。以下示例配置了一個 DedupeResponseHeader 過濾器:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: dedupe_response_header_route
          uri: https://example.org
          predicates:
          - Path=/hello
          filters:
          - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.dedupeResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.web.servlet.function.RequestPredicates.path;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsDedupeResponseHeader() {
        return route("dedupe_response_header_route")
                .route(path("/hello"), http())
                .before(uri("https://example.org"))
                .after(dedupeResponseHeader("Access-Control-Allow-Credentials Access-Control-Allow-Origin"))
                .build();
    }
}

當閘道器的 CORS 邏輯和下游邏輯都添加了 Access-Control-Allow-CredentialsAccess-Control-Allow-Origin 響應頭部時,此過濾器會移除其重複值。

DedupeResponseHeader 過濾器還接受一個可選的 strategy 引數。接受的值有 RETAIN_FIRST(預設)、RETAIN_LASTRETAIN_UNIQUE