AddRequestParameter Filter

AddRequestParameter Filter 使用 namevalue 参数。以下示例配置了一个 AddRequestParameter 过滤器:

application.yml

spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: add_request_parameter_route
          uri: https://example.org
          filters:
          - AddRequestParameter=red, blue
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> gatewayRouterFunctionsAddReqHeader() {
		return route("add_request_parameter_route")
				.GET("/anything/addrequestparam", http("https://example.org"))
					.before(addRequestParameter("red", "blue"))
					.build();
    }
}

这将为所有匹配请求将 red=blue 添加到下游请求的查询字符串。

AddRequestParameter 了解用于匹配路径或主机的 URI 变量。URI 变量可用于值中,并在运行时进行扩展。以下示例配置了一个使用变量的 AddRequestParameter 过滤器:

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;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
		return route("add_request_parameter_route")
				.route(host("{segment}.myhost.org"), http("https://example.org"))
				.before(addRequestParameter("foo", "bar-{segment}"))
				.build();
    }
}