DedupeResponseHeader 過濾器
DedupeResponseHeader 閘道器過濾器工廠接受一個 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-Credentials 和 Access-Control-Allow-Origin 響應頭的情況下,移除它們的重複值。
DedupeResponseHeader 過濾器還接受一個可選的 strategy 引數。可接受的值為 RETAIN_FIRST(預設)、RETAIN_LAST 和 RETAIN_UNIQUE。