StripPrefix フィルター

StripPrefix フィルターは 1 つのパラメーター 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: スキームハンドラーは、フィルターを最も高い優先順位に自動的に配置します。