PrefixPath 過濾器

PrefixPath 過濾器接受一個 prefix 引數。以下示例配置了一個 PrefixPath 過濾器。

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: prefixpath_route
          uri: https://example.org
          predicates:
          - Path=/**
          filters:
          - PrefixPath=/mypath
GatewaySampleApplication.java
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: 方案處理器會自動將過濾器置於最高優先順序。
© . This site is unofficial and not affiliated with VMware.