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: closed
- Created 6 years ago
- Reactions: 6
- Comments: 19 (3 by maintainers)
Commits related to this issue
- Merge pull request #2984 from Guofuyinan/main Support setting Spring.webflux.base-path and supporting it in path predicate Fixes #1759 — committed to spring-cloud/spring-cloud-gateway by ryanjbaxter 2 years ago
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=1Since I have also wasted almost a week of my time because of this issue, here is my solution:
GatwayFilter which fixes the webflux base-path issue
This filter must be used on all gateway routes with the correct number of parts. As of my understanding parts is the number of the gateway route path filter predicate.
Example usage in application configuration yaml
Hope this helps others who encounter the same problem 😃
I 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.
This is a fairly frustrating difference between how the MVC and Flux variants behave.
I started with an MVC gateway as I had a preexisting security implementation that was MVC based. I however encountered an issue with the MVC implementation so decided to try the Flux gateway.
After some time reworking my security layer, I came to find this issue. I have attempted a number of the suggested workarounds, however, with an ingress, Springdoc Flux, and other variables, I am stuck in a place where things are not working as expected. Things that were working fine with the MVC gateway.
The most desirable change would be to get the Flux variant to use the
base-pathmuch like the MVC variant uses theservlet-context-path.I unfortunately will have to either revert back to MVC version and “deal” with the aforementioned bug, or consider an alternative api-gateway.
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).