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: 方案處理程式會自動將過濾器置於最高優先順序。 |