MapRequestHeader 过滤器

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

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: map_request_header_route
          uri: https://example.org
          predicates:
          - Path=/mypath
          filters:
          - MapRequestHeader=Blue, X-Request-Red
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.mapRequestHeader;
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;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsMapRequestHeader() {
        return route("map_request_header_route")
				.GET("/mypath", http())
                .before(uri("https://example.org"))
				.before(mapRequestHeader("Blue", "X-Request-Red"))
				.build();
    }
}

这将向下游请求添加 X-Request-Red:<values> 头,其值已从传入 HTTP 请求的 Blue 头中更新。

© . This site is unofficial and not affiliated with VMware.