AddRequestHeader
Filter
AddRequestHeader
是一个 "before" 过滤器,它采用一个 name
和 value
参数。以下示例配置一个 AddRequestHeader
过滤器:
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: add_request_header_route
uri: https://example.org
filters:
- AddRequestHeader=X-Request-red, blue
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.addRequestHeader;
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> gatewayRouterFunctionsAddReqHeader() {
return route(GET("/red"), http("https://example.org"))
.before(addRequestHeader("X-Request-red", "blue"));
}
}
此清单将 X-Request-red:blue
标头添加到所有匹配请求的下游请求标头中。
AddRequestHeader
了解用于匹配路径或主机的 URI 变量。URI 变量可以用于该值并在运行时展开。以下示例配置一个使用变量的 AddRequestHeader
过滤器:
GatewaySampleApplication.java
@Configuration
class RouteConfiguration {
@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
return route(GET("/red/{segment}"), http("https://example.org"))
.before(addRequestHeader("X-Request-red", "blue-{segment}"));
}
}