Webflux base path does not work with Path predicates

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jigneshkhatricommented, Sep 17, 2021

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, for StripBasePathGatewayFilterFactory, filter name will be StripBasePath, and to use it in application.properties (or yaml) file, we need to define it like - spring.cloud.gateway.routes[0].filters=StripBasePath=1

2reactions
qelancommented, Jun 20, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context Path with Webflux - Stack Overflow
I was facing a similar issue with the spring.webflux.base-path (which didn't seem to be working as expected) in webflux-reactive-spring-web ...
Read more >
Spring Cloud Gateway
It does not work in a traditional Servlet Container or when built as a WAR. ... The Path Route Predicate Factory takes two...
Read more >
ignore server.servlet.context-path for webflux projects, support ...
In these project the context path should be set using the ``spring.webflux.base-path`` configuration property: ##server.servlet.context-path=/mypp ...
Read more >
URL Rewriting With Spring Cloud Gateway - Baeldung
Notice that only hostname/port are considered, as the final path comes from the rewrite logic. The predicates property defines the conditions ...
Read more >
Spring WebFlux - Spring Reactive Programming - DigitalOcean
If you look at above code, there is nothing related to Spring WebFlux. But Spring Boot will configure our application as Spring WebFlux...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found