Spring Gateway 内置断言笔记

编程 / Spring / 2022-02-28

以3.1.1为例,Spring网关内置了12种,用来匹配HTTP不同属性的断言工厂,可以直接使用配置的形式来使用,并且可以多个联合使用。

配置文件

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

配置类

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("news2", r -> r.path("/guoji").uri("http://news.baidu.com"))
                .build();
    }
}

内置断言

After Route Predicate Factory

匹配在指定日期时间之后发起的请求,参数是 [ZoneDateTime] 对象

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

Before Route Predicate Factory

匹配在指定日期之前发起的请求,参数是 [ZoneDateTime] 对象

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

Between Route Predicate Factory

匹配在指定日期中间发起的请求,参数是两个 [ZoneDateTime] 对象

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

匹配带有指定 Cookie 数据的请求,有两个参数,第一个是 Cookie 名称,后面是一个正则表达式,用来匹配 Cookie 的内容

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

Header Route Predicate Factory

匹配带有指定 Header 的请求,有两个参数,第一个是 Header 名称,后面是一个正则表达式,用来匹配 Header 的内容

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

Host Route Predicate Factory

匹配请求中 Host 数据,采用的是 Ant-Style 方式匹配,多个用英文逗号分隔

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

Method Route Predicate Factory

匹配指定方式的请求,参数是 HTTP Methods

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

Path Route Predicate Factory

匹配指定的路径请求,这也是最常用的一个断言。有两个参数:路径表达式(Spring PathMatcher patterns)集合和一个名为matchTrailingSlash(默认为true)的参数。

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

如果请求路径是,则此路由匹配,例如:/red/1or /red/1//red/blueor /blue/green
如果matchTrailingSlash设置为false,则请求路径/red/1/将不匹配。

Query Route Predicate Factory

匹配带有指定参数的请求,有两个参数,第一个是参数名,第二个是正则参数值(可选)
第一种情况:带green参数的请求,例如:http://localhost/color?green=1

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

第二种情况:带有red参数名和gree.正则的请求,例如:http://localhost/color?red=gree

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

RemoteAddr Route Predicate Factory

匹配发起请求中 RemoteAddr 部分的数据,参数为IPv4 或 IPv6 的地址字符串

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

Weight Route Predicate Factory

将更多的请求,指向权重更高的路由。有两个参数,第一个为组名,第二个为该组的权重

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

此配置会将80%的请求,转发至 https://weighthigh.org ;将20%请求,转发至 https://weightlow.org

XForwarded Remote Addr Route Predicate Factory

匹配请求中 X-Forwarded-For 的数据,参数为 IPv4 或 IPv6 的单个字符串或集合。

spring:
  cloud:
    gateway:
      routes:
      - id: xforwarded_remoteaddr_route
        uri: https://example.org
        predicates:
        - XForwardedRemoteAddr=192.168.1.1/24