spring-cloud-gateway: Webflux base path does not work with Path predicates
Describe the bug
When setting a spring.webflux.base-path property all routes using Path predicates return 404 results
Sample
application.name: demo
server.port: 8080
spring.webflux.base-path: /myapp
spring.cloud.gateway:
routes:
- id: google-route
uri: https://www.google.com/
predicates:
- Path=/api/**
filters:
- StripPrefix=1
Expected: Visit localhost:8080/myapp/api to see the google homepage (albeit stripped down).
Actual: 404
After digging a bit, I think this is because the PathRoutePredicateFactory uses the URI getRawPath.
Instead it could use a ServerHttpRequest’s RequestPath then call pathWithinApplication, which is already set up correctly via the ContextPathCompositeHandler.
It is this ContextPathCompositeHandler which rejects the request earlier if you visit just localhost:8080/api, so the base-path property seems to be broken either way.
Is this change sensible? Or is there a separate feature which allows this functionality?
Thanks for your time.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 4
- Comments: 15 (3 by maintainers)
Ok, this has literally had me stuck for a full week until I found this ticket. The “promised” documentation changes do not exist in the current Spring Cloud Gateway documentation, so if you add a spring.webflux.base-path things just plain stop working with no explanation of why.
Also, use of a base path is a requirement for our application, because we are adapting an existing Zuul API Gateway, with many people already having access to existing URLs. So, not using spring.webflux.base-path is not an option. And, trying to add the base path to all of the paths in the application also do not work, because things deeper like Actuator and Security start breaking if that is done.
Can someone please update the documentation to either include some of the workarounds in this ticket, or to give the “proper” way to have a base URL for the Spring Cloud Gateway?
The above solution provided by @skerdudou works just fine (though @sumitsarkar’s solution also looks similar but I haven’t checked on it). But for those wondering how to use (register) this custom filter, focus on the class name. The pattern in class name is like
{filter name}GatewayFilterFactory. So here, forStripBasePathGatewayFilterFactory, filter name will beStripBasePath, and to use it inapplication.properties(or yaml) file, we need to define it like -spring.cloud.gateway.routes[0].filters=StripBasePath=1I had similar issue and quite a similar solution as well, I created a filter to automatically strip the basePath in outgoing requests. This filter don’t need any parameter.
This was a hard requirement for us so I implemented a workaround:
Where the
StripsProxyPathFilterjust takes the substring of the url without the basePath.I’m interested why the team don’t want to re-implement this - what’s the past experience of zuul that influenced this decision?
Thanks.
Hello and thank you everyone for your work.
I want to mention that this problem still persists and we do not found an official way to handle this issue. When setting a spring.webflux.base-path property, we get a 404 on a simple (previously working) route:
We are on Spring-boot 3.0.5 & Spring-cloud 2022.0.2 (should be the latests at the moment).