spring-cloud/spring-cloud-gateway

Support paths on Route URIs as syntactic sugar for the `SetPath` filter

Open

#3,293 建立於 2024年3月11日

在 GitHub 查看
 (0 留言) (0 反應) (0 負責人)Java (4,284 star) (3,204 fork)batch import
enhancementhelp wanted

描述

There have been multiple requests and feedback regarding the ignoring of paths on Route URIs. Part of my hesitancy is how to support it across ALL means of configuring routes both reactive and MVC: config props, Java DSL and actuator.

I'm comfortable proposing it as syntactic sugar for the SetPath filter. There would be a mechanism to remove the path from the URI and insert a SetPath filter with that URI. There would be NO customization, at that point, break out to the original SetPath configuration.

The two routes in each of the following examples would be equivalent:

spring:
  cloud:
    gateway:
      routes:
      - id: set_path
        uri: http://example.com
        predicates:
        - Path=/foo/{segment}
        filters:
        - SetPath=/{segment}
      - id: set_path_uri
        uri: http://example.com/{segment}
        predicates:
        - Path=/foo/{segment}

reactive Java DSL

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
  return builder.routes()
      .route("setpath", p -> p.path("/foo/{segment}")
          .filters(f -> f.setPath("/{segment}"))
          .uri("http://example.com"))
      .route("setpathuri", p -> p.path("/foo/{segment}")
          .uri("http://example.com/{segment}"))
      .build();
}

MVC Java DSL

@Bean
public RouterFunction<ServerResponse> setPathRoute() {
  return route("setpath")
      .route(path("/foo/{segment}"), http("http://example.com"))
        .before(setPath("/{segment}"))
      .build().and(route()
        .route(path("/foo/{segment}"), http("http://example.com/{segment}"))
      .build());
}

/cc @joshlong

貢獻者指南