Search code examples
springroutesspring-cloudspring-cloud-netflixspring-cloud-gateway

Spring Cloud Gateway : disable default routes


I'm using spring cloud to manage my microservices.

For security reasons, for one specific microservice (name it ms_secure), I want to use custom route choose a specific microservice version depending on client IP.

My gateway config looks like this:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: ms_secure_v1
          uri: lb://ms_secure_v1
          predicates:
            - Path=/ms_secure/**
          filters:
            - RewritePath=/ms_secure/(?<segment>.*), /$\{segment}
            - name: <my filter>
              args:
                xForwardedForHeaderName: X-Forwarded-For
                hosts:
                  - <IP1>
                  - <IP2>

        - id: ms_secure
          uri: lb://ms_secure_v2
          predicates:
            - Path=/ms_secure/**
          filters:
            - RewritePath=/ms_secure/(?<segment>.*), /$\{segment}
            - name: <my filter>
              args:
                xForwardedForHeaderName: X-Forwarded-For
                hosts:
                  - <IP3>
                  - <IP4>

When when requesting /ms_secure:

  • IP1 and IP2 will be redirected to ms_secure_v1
  • IP3 and IP4 will be redirected to ms_secure_v2

My problem is that all my clients will also be able to access directly ms_secure_v1 or ms_secure_v2 by using the default routes:

http:///ms_secure_v1/...

http:///ms_secure_v2/...

I tried to disable these routes by using SetStatus GatewayFilter:

        - id: setstatusstring_route
          uri: lb://ms-gateway
          predicates:
            - Path=/ms_secure_v**
          filters:
          - SetStatus=403

But this route is not matched.

Is there a way to disable these default routes in spring gateway?


Solution

  • The following creates routes in gateway based on services registered:

    spring:
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
    

    Set it to false (which is the default), if you don't want this.