SetResponseHeader
Filter
SetResponseHeader
过滤器采用 name
和 value
参数。以下清单配置了一个 SetResponseHeader
过滤器:
The SetResponseHeader
filter takes name
and value
parameters.
The following listing configures a SetResponseHeader
filter:
spring:
cloud:
gateway:
mvc:
routes:
- id: setresponseheader_route
uri: https://example.org
filters:
- SetResponseHeader=X-Response-Red, Blue
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.setResponseHeader;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
@Configuration
class RouteConfiguration {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsSetResponseHeader() {
return route("addresponseheader")
.GET("/anything/addresheader", http("https://example.org"))
.after(setResponseHeader("X-Response-Red", "Blue"))
.build();
}
}
此 GatewayFilter 替换(而不是添加)所有具有给定名称的标头。因此,如果下游服务器用 X-Response-Red:1234
响应,它将被替换为 X-Response-Red:Blue
,这是网关客户端将收到的内容。
This GatewayFilter replaces (rather than adding) all headers with the given name.
So, if the downstream server responded with X-Response-Red:1234
, it will be replaced with X-Response-Red:Blue
, which is what the gateway client would receive.
SetResponseHeader
了解用于匹配路径或主机的 URI 变量。URI 变量可用于值,并会在运行时进行展开。以下示例配置了一个使用变量的 SetResponseHeader
过滤器:
SetResponseHeader
is aware of URI variables used to match a path or host.
URI variables may be used in the value and will be expanded at runtime.
The following example configures an SetResponseHeader
filter that uses a variable:
spring:
cloud:
gateway:
routes:
- id: setresponseheader_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- SetResponseHeader=foo, bar-{segment}
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.setResponseHeader;
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.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;
@Configuration
class RouteConfiguration {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsSetResponseHeader() {
return route("add_response_header_route")
.route(host("{segment}.myhost.org"), http("https://example.org"))
.after(setResponseHeader("foo", "bar-{segment}"))
.build();
}
}