Fixes rewrite path example and discovery (#2144)

Given that we would like to make the following rewriting (in both discovery and declarative context)

- '/foo'  to '/'
- '/foo/' to '/'
- '/foo/anything/else' to '/anything/else'

We need to

- force the presence of a leading '/' after rewriting as we may result in an empty uri path otherwise
- exclude it from 'remaining' group

See #1282
See #1359
See #1360
This commit is contained in:
Benjamin Einaudi
2021-03-11 00:26:34 +01:00
committed by GitHub
parent 37cb0ea27b
commit 68531abffe
4 changed files with 98 additions and 5 deletions

View File

@@ -1132,7 +1132,7 @@ spring:
predicates:
- Path=/red/**
filters:
- RewritePath=/red(?<segment>/?.*), $\{segment}
- RewritePath=/red/?(?<segment>.*), /$\{segment}
----
====
@@ -2226,7 +2226,7 @@ By default, the gateway defines a single predicate and filter for routes created
The default predicate is a path predicate defined with the pattern `/serviceId/**`, where `serviceId` is
the ID of the service from the `DiscoveryClient`.
The default filter is a rewrite path filter with the regex `/serviceId/(?<remaining>.*)` and the replacement `/${remaining}`.
The default filter is a rewrite path filter with the regex `/serviceId/?(?<remaining>.*)` and the replacement `/${remaining}`.
This strips the service ID from the path before the request is sent downstream.
If you want to customize the predicates or filters used by the `DiscoveryClient` routes, set `spring.cloud.gateway.discovery.locator.predicates[x]` and `spring.cloud.gateway.discovery.locator.filters[y]`.
@@ -2244,7 +2244,7 @@ spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com
spring.cloud.gateway.discovery.locator.filters[0].name: CircuitBreaker
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/?(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"
----
====