Webflux base path does not work with Path predicates
See original GitHub issueDescribe 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:11 (3 by maintainers)
Top Related StackOverflow Question
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=1Ok, 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?