Spring Cloud Gateway – Routing Predicate Factory

1. The After Route Predicate Factory

When the requested time is after the set time, the request can access the service, if the request is accessed before the set time, return 404

server:
  port: 10020
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: lb://consumer-service
           #When the request time is after the set time, the request will be forwarded to the microservice
           #Request to access before the set time returns 404
          predicates:
            - After=2023-07-14T17:50:42.579 + 08:00[Asia/Shanghai]

Set time > current time return 404

  • Setting Set time>Current time Return 404

predicates:
– After=2023-07-14T17:59:42.579+08:00[Asia/Shanghai]

2. The Before Route Predicate Factory

Only when the request time does not exceed the set time can the service be accessed. If the request exceeds the time, 404 will be returned

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: before_route
          uri: lb://consumer-service
         
          predicates:
            - Before=2023-07-14T18:55:42.579 + 08:00[Asia/Shanghai]

  • Modify the set time < current time

3. The Bwtween Route Predicate Factory

If the request time is within the set time, the request can access the service; if it is not between the set time, the service will not be returned directly

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: between_route
          uri: lb://consumer-service
          #Set the time interval
          predicates:
            - Between=2023-07-14T18:45:42.579 + 08:00[Asia/Shanghai],2023-07-14T18:59:42.579 + 08:00[Asia/Shanghai]

  • Modify the time interval, the current time is not included in the time interval

– Between=2023-07-14T18:45:42.579 + 08:00[Asia/Shanghai],2023-07-14T18:50:42.579 + 08:00[Asia/Shanghai]

4. The Cookie Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: cookie_route
          uri: lb://consumer-service
          #Set the regular expression that the cookie name and cookie value of the access request match
          predicates:
            - Cookie=mycookie,[a-zA-Z]
  • Use postman to access, set the cookie of the access request

  • Modify the value of the cookie

  • Modify the name of the cookie

5. The Header Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: Header_route
          uri: lb://consumer-service
          #Set the regular expression that the name of the request header matches the value of the request header
          #Only if the name of the request is consistent with the setting and the value of the request header satisfies the regular expression, the request can be sent to the service
          predicates:
            -Header=Request-Id,[a-zA-Z]

  • Modify the value in the request header

  • Modify the name in the request header

6. The Host Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: host_route
          uri: lb://consumer-service
          #Only if the name of the request header is host and conforms to **.somehost, the request can be forwarded to the service
          predicates:
            - Host=**.somehost

  • Modify the value of Host in the request header

  • Modify the key name in the request header

7. The Method Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: method_route
          uri: lb://consumer-service
          #Set the request method that can be accessed
          predicates:
            -Method=GET,POST

8. The Path Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: path_route
          uri: lb://consumer-service
          #Set the path to allow access
          # Only when the request path is /consumer/**, the request can access the service
          predicates:
            -Path=/consumer/**

9. The Query Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: query_route
          uri: lb://consumer-service
          #Set the name of the carrying parameter that is allowed to access
          #Only if the request carries parameters with data, the request can access the service
          predicates:
            - Query=data

  • Modify the name of the parameter

10. The RemoteAddr Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: remoteAddr_route
          uri: lb://consumer-service
          #Set the ip address that is allowed to access
          #Only if the accessed ip address is within this network segment, the request can access the service
          predicates:
            - RemoteAddr=127.0.0.1/10