Deny access to one particular subpath for spring cloud gateway route
Asked Answered
I

2

5

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?

Inosculate answered 5/3, 2019 at 12:37 Comment(0)
P
7
  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
Parvenu answered 5/3, 2019 at 15:36 Comment(2)
Ah, a separate route. I was fixated on trying to update the predicate on the first route. Thanks spencergibb!Inosculate
I have the same requirement, but to do with the api code.Genaro
G
1

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();
}
Genaro answered 23/9, 2022 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.