StripPrefix Filter

StripPrefix 过滤器使用一个参数 partsparts 参数指示在将请求发送到下游之前从请求中剥离路径中的部分数。以下列表配置了一个 StripPrefix 过滤器:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: nameRoot
          uri: https://nameservice
          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.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsStripPrefix() {
		return route("nameRoot")
				.GET("/name/**", http("https://example.org"))
					.before(stripPrefix(2))
					.build();
    }
}

当通过网关向 /name/blue/red 发出请求时,向 nameService 发出的请求看上去就像 https://nameservice/red