Spring Cloud Gateway 路由谓词工厂

Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础结构的一部分进行匹配。Spring Cloud Gateway包含许多内置的Route Predicate工厂。所有这些谓词都匹配HTTP请求的不同属性。多路线谓词工厂可以组合并通过逻辑和组合。

After Route Predicate Factory

After Route Predicate Factory采用一个参数,一个日期时间。此谓词匹配在当前日期时间之后发生的请求。

application.yml

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

 此Route与2017年1月20日17:42 Mountain Time(Denver)之后的所有要求相匹配。

Before Route Predicate Factory

Before Route Predicate Factory采用一个参数,一个日期时间。此谓词匹配在当前日期时间之前发生的请求。

application.yml

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

此路由与2017年1月20日17:42 Mountain Time(Denver)之前的任何要求相匹配。 

Between Route Predicate Factory

Between Route Predicate Factory采用两个参数,datetime1和datetime2。此谓词匹配datetime1之后和datetime2之前发生的请求。datetime2参数必须在datetime1之后。

application.yml

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

此谓词工厂与2017年1月20日17:42山地时间(丹佛)之后和2017年1月21日17:42山区时间(丹佛)之后的任何请求相匹配。这对维护窗口很有用 

Cookie Route Predicate Factory有两个参数,cookie名称和正则表达式。此谓词匹配具有给定名称且值与正则表达式匹配的cookie。

application.yml

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

此路由匹配请求有一个名为chocolate的cookie,其值与ch.p正则表达式匹配。

Header Route Predicate Factory

Header Route Predicate Factory采用两个参数,标题名称和正则表达式。此谓词与具有给定名称且值与正则表达式匹配的标头匹配。

application.yml

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

如果请求具有名为X-Request-Id的标头,其值与\ d +正则表达式匹配(具有一个或多个数字的值),则此路由匹配。

Host Route Predicate Factory

Host Route Predicate Factory采用一个参数:主机名模式列表。该模式是一种Ant样式模式。作为分隔符。此谓词匹配与模式匹配的Host标头。

application.yml

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

也支持URI模板变量,例如{sub} .myhost.org。

如果请求的主机头具有值www.somehost.org或beta.somehost.org或www.anotherhost.org,则此路由将匹配。

此谓词将URI模板变量(如上例中定义的sub)提取为名称和值的映射,并将其放在ServerWebExchange.getAttributes()中,并在ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE中定义一个键。然后,这些值可供GatewayFilter Factories使用。

Method Route Predicate Factory

Method Route Predicate Factory采用一个参数:要匹配的HTTP方法。

application.yml

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

如果请求方法是GET,则此路由将匹配。

Path Route Predicate Factory

Path Route Predicate Factory有两个参数:Spring PathMatcher模式列表和matchOptionalTrailingSeparator的可选标志。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://example.org
        predicates:
        - Path=/foo/{segment},/bar/{segment}

 如果请求路径是,则此路由将匹配,例如:/ foo / 1或/ foo / bar或/ bar / baz。

此谓词将URI模板变量(如上例中定义的段)提取为名称和值的映射,并将其放在ServerWebExchange.getAttributes()中,并在ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE中定义一个键。

然后,这些值可供GatewayFilter Factories使用可以使用实用程序方法来更轻松地访问这些变量。

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);

String segment = uriVariables.get("segment");

Query Route Predicate Factory

Query Route Predicate Factory有两个参数:一个必需的参数和一个可选的正则表达式。

application.yml

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

如果请求包含baz查询参数,则此路由将匹配。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://example.org
        predicates:
        - Query=foo, ba.

 如果请求包含其值与ba匹配的foo查询参数,则此路由将匹配。regexp,所以bar和baz匹配。

RemoteAddr Route Predicate Factory

RemoteAddr Route Predicate Factory采用CIDR表示法(IPv4或IPv6)字符串的列表(最小值为1),例如,192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)。

application.yml

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

如果请求的远程地址是例如192.168.1.10,则此路由将匹配。

修改远程地址的解析方式 

默认情况下,RemoteAddr Route Predicate Factory使用传入请求中的远程地址。如果Spring Cloud Gateway位于代理层后面,则可能与实际客户端IP地址不匹配。

您可以通过设置自定义RemoteAddressResolver来自定义解析远程地址的方式。Spring Cloud Gateway附带一个非默认远程地址解析器,它基于X-Forwarded-For标头XForwardedRemoteAddressResolver。

XForwardedRemoteAddressResolver有两个静态构造函数方法,它们采用不同的安全方法:XForwardedRemoteAddressResolver :: trustAll返回一个RemoteAddressResolver,它始终采用X-Forwarded-For标头中找到的第一个IP地址。这种方法容易受到欺骗,因为恶意客户端可以为解析器接受的X-Forwarded-For设置初始值。XForwardedRemoteAddressResolver :: maxTrustedIndex采用与Spring Cloud Gateway前运行的可信基础架构数相关的索引。例如,如果只能通过HAProxy访问Spring Cloud Gateway,则应使用值1。如果在可访问Spring Cloud Gateway之前需要两跳可信基础架构,则应使用值2。

给出以下标头值:

X-Forwarded-For: 0.0.0.1, 0.0.0.2, 0.0.0.3

下面的maxTrustedIndex值将产生以下远程地址。

maxTrustedIndex result

[Integer.MIN_VALUE,0]

(invalid, IllegalArgumentException during initialization)

1

0.0.0.3

2

0.0.0.2

3

0.0.0.1

[4, Integer.MAX_VALUE]

0.0.0.1

使用Java配置:

GatewayConfig.java 

RemoteAddressResolver resolver = XForwardedRemoteAddressResolver
    .maxTrustedIndex(1);

...

.route("direct-route",
    r -> r.remoteAddr("10.1.1.1", "10.10.1.1/24")
        .uri("https://downstream1")
.route("proxied-route",
    r -> r.remoteAddr(resolver,  "10.10.1.1", "10.10.1.1/24")
        .uri("https://downstream2")
)

猜你喜欢

转载自blog.csdn.net/u013702678/article/details/88710943