SetPath Filter

SetPath 过滤器采用路径 template 参数。它通过允许路径的模板区段,提供了一种简单的方法来操作请求路径。这使用 Spring 框架的 URI 模板。允许多个匹配区段。以下示例配置了 SetPath 过滤器:

The SetPath filter takes a path template parameter. It offers a simple way to manipulate the request path by allowing templated segments of the path. This uses the URI templates from Spring Framework. Multiple matching segments are allowed. The following example configures a SetPath filter:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: setpath_route
          uri: https://example.org
          predicates:
          - Path=/red/{segment}
          filters:
          - SetPath=/{segment}
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.setPath;
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> gatewayRouterFunctionsSetPath() {
		return route("add_request_parameter_route")
				.GET("/red/{segment}", http("https://example.org"))
					.before(setPath("/{segment"))
					.build();
    }
}

对于请求路径 /red/blue,它在发出下游请求之前将路径设置为 /blue

For a request path of /red/blue, this sets the path to /blue before making the downstream request.