PrefixPath 过滤器
PrefixPath Filter
PrefixPath 过滤器接受一个 prefix 参数。以下示例配置了一个 PrefixPath 过滤器:
spring:
cloud:
gateway:
server:
webmvc:
routes:
- id: prefixpath_route
uri: https://example.org
predicates:
- Path=/**
filters:
- PrefixPath=/mypath
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.prefixPath;
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> gatewayRouterFunctionsPrefixPath() {
return route("prefixpath_route")
.GET("/**", http())
.before(uri("https://example.org"))
.before(prefixPath("/mypath"))
.build();
}
}
这会将 /mypath 前缀添加到所有匹配请求的路径前。因此,对 /hello 的请求会被发送到 /mypath/hello。
注意
如果使用 lb() 过滤器,需要将其放在 prefixPath() 过滤器之后,否则生成的 URL 可能不正确。配置中的 lb: 方案处理器会自动将该过滤器置于最高优先级顺序。