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-Credentials
和 Access-Control-Allow-Origin
响应头部时,此过滤器会移除其重复值。
DedupeResponseHeader
过滤器还接受一个可选的 strategy
参数。接受的值有 RETAIN_FIRST
(默认)、RETAIN_LAST
和 RETAIN_UNIQUE
。