StripPrefix
過濾器
StripPrefix
過濾器接受一個引數,即 parts
。parts
引數指示在將請求傳送到下游之前,要從請求路徑中剝離的部分數量。以下清單配置了一個 StripPrefix
過濾器
application.yml
spring:
cloud:
gateway:
mvc:
routes:
- id: strip_prefix_route
uri: https://example.org
predicates:
- Path=/name/**
filters:
- StripPrefix=2
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.stripPrefix;
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> gatewayRouterFunctionsStripPrefix() {
return route("strip_prefix_route")
.GET("/name/**", http())
.before(uri("https://example.org"))
.before(stripPrefix(2))
.build();
}
}
當透過閘道器向 /name/blue/red
發出請求時,完整的請求 URL 看起來像 example.org/red
。
如果使用 lb() 過濾器,它需要在 stripPrefix() 過濾器之後,否則生成的 URL 可能會不正確。配置中的 lb: Scheme 處理程式會自動將該過濾器置於最高優先順序。 |