spring cloud gateway系列教程1—Route Predicate

spring cloud gateway系列教程目录

  1. spring cloud gateway系列教程1—Route Predicate
  2. spring cloud gateway系列教程2——GatewayFilter_上篇
  3. spring cloud gateway系列教程2——GatewayFilter_下篇
  4. spring cloud gateway系列教程3—Global Filters
  5. spring cloud gateway系列教程4—其余配置

怎么引入spring cloud gateway

maven上,只需引入以下依赖便可:java

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

引入了依赖默认即开启gateway了,若是暂时不想使用这个功能,这能够配置spring.cloud.gateway.enabled=false便可。web

Spring Cloud Gateway使用的是Spring Boot和Spring Webflux提供的Netty底层环境,不能和传统的Servlet容器一块儿使用,也不能打包成一个WAR包。正则表达式

工做原理

当客户端发送请求到Spring Cloud Gateway,Gateway Handler Mapping会匹配Route映射分发到Gateway Web Handler。handler会将请求通过一系列的filter处理,代理请求前,会执行左右的"pre" filter逻辑,代理请求后,会执行全部"post" filter逻辑。spring

Route Predicate Factories

Spring Cloud Gateway是使用Spring WebFlux的HandlerMapping做为匹配路由底层实现,自己已自带不少Route Predicate Factories,分别匹配不一样的http请求属性,多个Route Predicate Factories也能够经过and进行逻辑合并匹配。安全

1. After Route Predicate Factory

After Route Predicate Factory使用的是时间做为匹配规则,只要当前时间大于设定时间,路由才会匹配请求。
application.ymlcookie

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://www.google.com
        predicates:
        - After=2018-12-25T14:33:47.789+08:00

这个路由规则会在东8区的2018-12-25 14:33:47后,将请求都转跳到google。app

2. Before Route Predicate Factory

Before Route Predicate Factory也是使用时间做为匹配规则,只要当前时间小于设定时间,路由才会匹配请求。
application.ymlmaven

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://www.google.com
        predicates:
        - Before=2018-12-25T14:33:47.789+08:00

这个路由规则会在东8区的2018-12-25 14:33:47前,将请求都转跳到google。svg

3. Between Route Predicate Factory

Between Route Predicate Factory也是使用两个时间做为匹配规则,只要当前时间大于第一个设定时间,并小于第二个设定时间,路由才会匹配请求。post

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://www.google.com
        predicates:
        - Between=2018-12-25T14:33:47.789+08:00, 2018-12-26T14:33:47.789+08:00

这个路由规则会在东8区的2018-12-25 14:33:47到2018-12-26 14:33:47之间,将请求都转跳到google。

4. Cookie Route Predicate Factory

Cookie Route Predicate Factory使用的是cookie名字和正则表达式的value做为两个输入参数,请求的cookie须要匹配cookie名和符合其中value的正则。
application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://www.google.com
        predicates:
        - Cookie=cookiename, cookievalue

路由匹配请求存在cookie名为cookiename,cookie内容匹配cookievalue的,将请求转发到google。

5. Header Route Predicate Factory

Header Route Predicate Factory,与Cookie Route Predicate Factory相似,也是两个参数,一个header的name,一个是正则匹配的value。
application.yml

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

路由匹配存在名为X-Request-Id,内容为数字的header的请求,将请求转发到google。

6. Host Route Predicate Factory

Host Route Predicate Factory使用的是host的列表做为参数,host使用Ant style匹配。
application.yml

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

路由会匹配Host诸如:www.somehost.orgbeta.somehost.orgwww.anotherhost.org等请求。

7. Method Route Predicate Factory

Method Route Predicate Factory是经过HTTP的method来匹配路由。
application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://www.google.com
        predicates:
        - Method=GET

路由会匹配到全部GET方法的请求。

8. Path Route Predicate Factory

Path Route Predicate Factory使用的是path列表做为参数,使用Spring的PathMatcher匹配path,能够设置可选变量。
application.yml

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

上面路由能够匹配诸如:/foo/1/foo/bar/bar/baz
其中的segment变量能够经过下面方式获取:

PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE);
Map<String, String> uriVariables = variables.getUriVariables();
String segment = uriVariables.get("segment");

在后续的GatewayFilter Factories就能够作对应的操做了。

9. Query Route Predicate Factory

Query Route Predicate Factory能够经过一个或两个参数来匹配路由,一个是查询的name,一个是查询的正则value。
application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://www.google.com
        predicates:
        - Query=baz

路由会匹配全部包含baz查询参数的请求。
application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://www.google.com
        predicates:
        - Query=foo, ba.

路由会匹配全部包含baz,而且baz的内容为诸如:barbaz等符合ba.正则规则的请求。

10. RemoteAddr Route Predicate Factory

RemoteAddr Route Predicate Factory经过无类别域间路由(IPv4 or IPv6)列表匹配路由。
application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://www.google.com
        predicates:
        - RemoteAddr=192.168.1.1/24

上面路由就会匹配RemoteAddr诸如192.168.1.10等请求。

10.1 Modifying the way remote addresses are resolved

RemoteAddr Route Predicate Factory默认状况下,使用的是请求的remote address。可是若是Spring Cloud Gateway是部署在其余的代理后面的,如Nginx,则Spring Cloud Gateway获取请求的remote address是其余代理的ip,而不是真实客户端的ip。

考虑到这种状况,你能够自定义获取remote address的处理器RemoteAddressResolver。固然Spring Cloud Gateway也提供了基于X-Forwarded-For请求头的XForwardedRemoteAddressResolver
熟悉Http代理协议的,都知道X-Forwarded-For头信息作什么的,不熟悉的能够本身谷歌了解一下。

XForwardedRemoteAddressResolver提供了两个静态方法获取它的实例:
XForwardedRemoteAddressResolver::trustAll获得的RemoteAddressResolver老是获取X-Forwarded-For的第一个ip地址做为remote address,这种方式就比较容易被假装的请求欺骗,模拟请求很容易经过设置初始的X-Forwarded-For头信息,就能够欺骗到gateway。

XForwardedRemoteAddressResolver::maxTrustedIndex获得的RemoteAddressResolver则会在X-Forwarded-For信息里面,从右到左选择信任最多maxTrustedIndex个ip,由于X-Forwarded-For是越往右是越接近gateway的代理机器ip,因此是越往右的ip,信任度是越高的。
那么若是前面只是挡了一层Nginx的话,若是只须要Nginx前面客户端的ip,则maxTrustedIndex取1,就能够比较安全地获取真实客户端ip。

使用java的配置:
GatewayConfig.java

RemoteAddressResolver resolver = XForwardedRemoteAddressResolver
    .maxTrustedIndex(1);

...

.route("direct-route",
    r -> r.remoteAddr("10.1.1.1", "10.10.1.1/24")
        .uri("http://www.google.com")
.route("proxied-route",
    r -> r.remoteAddr(resolver,  "10.10.1.1", "10.10.1.1/24")
        .uri("http://www.google.com")
)

这一章节讲的了几种Route Predicate Factory的使用及场景,下一章节讲GatewayFilter Factories的使用

若是想查看其余spring cloud gateway的案例和使用,能够点击查看