spring-cloud-gateway
spring-cloud-gateway copied to clipboard
Add "ignore path" option, to exclude path from routing
Enhancement
Currently I see no way I can exclude particular path from routing. For instance if I want my gateway to expose some endpoints.
If I have the following route configuration (/** - matches everything):
- id: frontend
uri: ${com.siemens.cms.gateway.routes.frontend}
predicates:
- Path=/**
I want to be able to do something like the following (make some exclusions):
- id: frontend
uri: ${com.siemens.cms.gateway.routes.frontend}
redicates:
- Path=/**, except: /actuator/**
or
spring:
cloud:
gateway:
routes:
.....
ignore:
predicate:
- Path=/actuator/**
I think it would be very convenient to have such option.
Are there any updates on this? Is it possible to add such feature?
This was explained in https://github.com/spring-cloud/spring-cloud-gateway/issues/496#issuecomment-439986202
Isn't this issue (#1266) a duplicate of a feature request #309 identified for long time ?
Any update on this?
This was explained in #496 (comment)
The use case OP presented is not addressed by that comment. The presented solution of no://op
only means to stop routing and return an HTTP Status code, but it doesn't allow the gateway to handle the request via other means (e.g. local to the gateway).
One way I found to handle this case required using the fluent API since negate doesn't seem to be supported in java config unfortunately. It means removing it from the other routes in application.yml
, but I was ok with that for this special purpose route. With this configured the LOGOUT_HTML
and LOGOUT_CSS
paths are served from the static files on the classpath while all other unmatched paths proxy to the defaultHost.
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("default", r -> r
.order(Ordered.LOWEST_PRECEDENCE)
.path("/**")
.and().not(p -> p.path(LOGOUT_HTML, LOGOUT_CSS))
.filters(f -> f
# Any appropriate filters
)
.uri(defaultHost)
)
.build();
}
how about now?
Is there any update to this?
I guess a simple NotPath
predicate could do the trick:
@Component
public class NotPathRoutePredicateFactory extends PathRoutePredicateFactory {
@Override
public Predicate<ServerWebExchange> apply(final Config config) {
return super.apply(config).negate();
}
}
usage
- id: frontend
uri: ${com.siemens.cms.gateway.routes.frontend}
predicates:
- Path=/**
- NotPath=/actuator/**
not particularly pretty though