DedupeResponseHeader 篩選器

DedupeResponseHeader GatewayFilter 工廠接受 name 參數和選用的 strategy 參數。name 可以包含以空格分隔的標頭名稱清單。以下範例設定 DedupeResponseHeader 篩選器

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: dedupe_response_header_route
          uri: https://example.org
          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.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("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