GaZ
GaZ

Reputation: 2406

Deny access to one particular subpath for spring cloud gateway route

We're using Spring Cloud Gateway in front of our backend services. We have a route similar to the following:

  routes:
    - id: foobar-service
      uri: lb://foobar-service
      predicates:
        - Path=/foobar/**
      filters:
        - StripPrefix=1

We want to deny access to one particular subpath (e.g. /foobar/baz/**) but leave the rest open. Is it possible to do this using the YAML syntax? Perhaps we need to implement the routes using the Fluent API instead?

Upvotes: 5

Views: 3678

Answers (2)

jfk
jfk

Reputation: 5307

Sharing my experience with the API implementation with a single route.

@Bean
public RouteLocator routes( final RouteLocatorBuilder locatorBuilder ) {
    RouteLocatorBuilder.Builder builder = locatorBuilder.routes();
    builder.route(p -> p //
                    .path(getAllowedPaths()) //
                    .and() //
                    .not(n -> n.path(getRestrictedPaths()) //
                    .filters(f -> f //
                            //filters
                    .uri(uri)));
    return builder.build();
}

Upvotes: 1

spencergibb
spencergibb

Reputation: 25177

  routes:
    - id: foobar-baz-service
      uri: no://op
      predicates:
        - Path=/foobar/baz/**
      filters:
        - SetStatus=403
    - id: foobar-service
      uri: lb://foobar-service
      predicates:
        - Path=/foobar/**
      filters:
        - StripPrefix=1

Upvotes: 7

Related Questions