MapRequestHeader Filter

MapRequestHeader 过滤器采用 fromHeadertoHeader 参数。它创建一个新的命名头(toHeader),并且该值从进入 HTTP 请求中一个现有的命名头(fromHeader)中提取出来。如果输入头不存在,那么过滤器不会产生任何影响。如果新命名头已存在,那么它的值将以新值进行扩充。以下示例配置了 MapRequestHeader

The MapRequestHeader filter takes fromHeader and toHeader parameters. It creates a new named header (toHeader), and the value is extracted out of an existing named header (fromHeader) from the incoming http request. If the input header does not exist, the filter has no impact. If the new named header already exists, its values are augmented with the new values. The following example configures a MapRequestHeader:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: map_request_header_route
          uri: https://example.org
          filters:
          - MapRequestHeader=Blue, X-Request-Red
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.addRequestParameter;
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> gatewayRouterFunctionsMapRequestHeader() {
        return route("map_request_header_route")
				.GET("/mypath", http("https://example.org"))
				.before(mapRequestHeader("Blue", "X-Request-Red"))
				.build();
    }
}

这会向附带来自传入 HTTP 请求的 “Blue” 标头已更新值的向下请求添加 “X-Request-Red:<values>` 标头。

This adds the X-Request-Red:<values> header to the downstream request with updated values from the incoming HTTP request’s Blue header.