spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Add "ignore path" option, to exclude path from routing

Open mygordienko opened this issue 4 years ago • 5 comments

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.

mygordienko avatar Aug 26 '19 12:08 mygordienko

Are there any updates on this? Is it possible to add such feature?

mygordienko avatar Sep 22 '19 11:09 mygordienko

This was explained in https://github.com/spring-cloud/spring-cloud-gateway/issues/496#issuecomment-439986202

bratwurzt avatar May 11 '20 08:05 bratwurzt

Isn't this issue (#1266) a duplicate of a feature request #309 identified for long time ?

gberche-orange avatar Oct 12 '20 12:10 gberche-orange

Any update on this?

s-aliasgar avatar Apr 11 '22 06:04 s-aliasgar

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();
}

sfariaNG avatar Jul 08 '22 22:07 sfariaNG

how about now?

chen-junl avatar Feb 24 '23 03:02 chen-junl

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

sfussenegger avatar Apr 26 '23 13:04 sfussenegger