|
| 1 | +# SpringCloudGateway核心之Predicate路由工厂 |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +> Spring Cloud Gateway 将路由作为 Spring WebFluxHandlerMapping基础设施的一部分进行匹配。Spring Cloud Gateway 包含许多内置的路由Predicate 工厂。所有这些谓词都匹配 HTTP 请求的不同属性。多个 Route Predicate Factory 可以组合,并通过逻辑组合and。 |
| 6 | +## After Route Predicate Factory |
| 7 | +> After Route Predicate Factory 采用一个参数,即日期时间。此谓词匹配当前日期时间之后发生的请求。 |
| 8 | +
|
| 9 | +```yaml |
| 10 | +例子:此路线匹配 2017 年 1 月 20 日 17:42 山地时间(丹佛)之后的任何请求。 |
| 11 | +spring: |
| 12 | + cloud: |
| 13 | + gateway: |
| 14 | + routes: |
| 15 | + - id: after_route |
| 16 | + uri: https://example.org |
| 17 | + predicates: |
| 18 | + - After=2017-01-20T17:42:47.789-07:00[America/Denver] |
| 19 | +``` |
| 20 | +
|
| 21 | +## Before Route Predicate Factory |
| 22 | +> Before Route Predicate Factory 采用一个参数,即日期时间。此谓词匹配当前日期时间之前发生的请求。 |
| 23 | +
|
| 24 | +```yaml |
| 25 | +例子:此路线匹配 2017 年 1 月 20 日 17:42 山地时间(丹佛)之前的任何请求。 |
| 26 | +spring: |
| 27 | + cloud: |
| 28 | + gateway: |
| 29 | + routes: |
| 30 | + - id: before_route |
| 31 | + uri: https://example.org |
| 32 | + predicates: |
| 33 | + - Before=2017-01-20T17:42:47.789-07:00[America/Denver] |
| 34 | +``` |
| 35 | +## Between Route Predicate Factory |
| 36 | +>Between Route Predicate Factory 采用两个参数:datetime1 和 datetime2。此谓词匹配在 datetime1 之后和 datetime2 之前发生的请求。datetime2 参数必须位于 datetime1 之后。 |
| 37 | +
|
| 38 | +```yaml |
| 39 | +例子:此路线匹配 2017 年 1 月 20 日 17:42 山地时间(丹佛)之后和 2017 年 1 月 21 日 17:42 山地时间(丹佛)之前的任何请求。这对于维护窗口可能很有用。 |
| 40 | +spring: |
| 41 | + cloud: |
| 42 | + gateway: |
| 43 | + routes: |
| 44 | + - id: between_route |
| 45 | + uri: https://example.org |
| 46 | + predicates: |
| 47 | + - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver] |
| 48 | +``` |
| 49 | +## Cookie Route Predicate Factory |
| 50 | +> Cookie 路由谓词工厂采用两个参数:cookie 名称和正则表达式。此谓词匹配具有给定名称且值与正则表达式匹配的 cookie。 |
| 51 | +
|
| 52 | +```yaml |
| 53 | +例子:该路由匹配请求有一个名为chocolatewho 的值与ch.p正则表达式匹配的 cookie。 |
| 54 | +spring: |
| 55 | + cloud: |
| 56 | + gateway: |
| 57 | + routes: |
| 58 | + - id: cookie_route |
| 59 | + uri: https://example.org |
| 60 | + predicates: |
| 61 | + - Cookie=chocolate, ch.p |
| 62 | +``` |
| 63 | +## Header Route Predicate Factory |
| 64 | +> 标头路由谓词工厂采用两个参数:标头名称和正则表达式。此谓词与具有给定名称且值与正则表达式匹配的标头匹配。 |
| 65 | +
|
| 66 | +```yaml |
| 67 | +例子:如果请求具有名为X-Request-Idwhos 值与\d+正则表达式匹配的标头(具有一位或多位数字的值),则此路由匹配。 |
| 68 | +spring: |
| 69 | + cloud: |
| 70 | + gateway: |
| 71 | + routes: |
| 72 | + - id: header_route |
| 73 | + uri: https://example.org |
| 74 | + predicates: |
| 75 | + - Header=X-Request-Id, \d+ |
| 76 | +``` |
| 77 | +## Host Route Predicate Factory |
| 78 | +> 主机路由谓词工厂采用一个参数:主机名模式列表。该模式是一个 Ant 风格模式,以.分隔符作为分隔符。该谓词匹配Host与模式匹配的标头。 |
| 79 | +
|
| 80 | +```yaml |
| 81 | +例子:还支持 URI 模板变量,例如{sub}.myhost.org. |
| 82 | +如果请求的Host标头值为www.somehost.orgorbeta.somehost.org或,则此路由将匹配www.anotherhost.org。 |
| 83 | +该谓词提取 URI 模板变量(如上sub例中定义的)作为名称和值的映射,并将其放置在 中,ServerWebExchange.getAttributes()并使用 中定义的键ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE。这些值随后可供GatewayFilter Factory使用 |
| 84 | +spring: |
| 85 | + cloud: |
| 86 | + gateway: |
| 87 | + routes: |
| 88 | + - id: header_route |
| 89 | + uri: https://example.org |
| 90 | + predicates: |
| 91 | + - Header=X-Request-Id, \d+ |
| 92 | +``` |
| 93 | +## Method Route Predicate Factory |
| 94 | +> 方法路由谓词工厂采用一个参数:要匹配的 HTTP 方法。 |
| 95 | +
|
| 96 | +```yaml |
| 97 | +例子:如果请求方法是 ,则该路由将匹配GET。 |
| 98 | +spring: |
| 99 | + cloud: |
| 100 | + gateway: |
| 101 | + routes: |
| 102 | + - id: method_route |
| 103 | + uri: https://example.org |
| 104 | + predicates: |
| 105 | + - Method=GET |
| 106 | +``` |
| 107 | +## Path Route Predicate Factory |
| 108 | +> 路径路由谓词工厂采用两个参数:Spring 模式列表PathMatcher和可选标志matchOptionalTrailingSeparator。 |
| 109 | +
|
| 110 | +```yaml |
| 111 | +例子:如果请求路径是,例如:/foo/1or /foo/baror ,则该路由将匹配/bar/baz。 |
| 112 | +该谓词提取 URI 模板变量(如上segment例中定义的)作为名称和值的映射,并将其放置在 中,ServerWebExchange.getAttributes()并使用 中定义的键ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE。这些值随后可供GatewayFilter Factory使用 |
| 113 | +spring: |
| 114 | + cloud: |
| 115 | + gateway: |
| 116 | + routes: |
| 117 | + - id: host_route |
| 118 | + uri: https://example.org |
| 119 | + predicates: |
| 120 | + - Path=/foo/{segment},/bar/{segment} |
| 121 | +``` |
| 122 | +## Query Route Predicate Factory |
| 123 | +> 查询路由谓词工厂采用两个参数:必需的param和可选的regexp。 |
| 124 | +
|
| 125 | +```yaml |
| 126 | +例子:如果请求包含baz查询参数,则该路由将匹配。 |
| 127 | +spring: |
| 128 | + cloud: |
| 129 | + gateway: |
| 130 | + routes: |
| 131 | + - id: query_route |
| 132 | + uri: https://example.org |
| 133 | + predicates: |
| 134 | + - Query=baz |
| 135 | + |
| 136 | +``` |
| 137 | +## RemoteAddr Route Predicate Factory |
| 138 | +> RemoteAddr 路由谓词工厂采用 CIDR 表示法(IPv4 或 IPv6)字符串的列表(最小大小为 1),例如192.168.0.1/16(其中192.168.0.1是 IP 地址,16是子网掩码)。 |
| 139 | +
|
| 140 | +```yaml |
| 141 | +例子:如果请求的远程地址是 ,则该路由将匹配192.168.1.10 |
| 142 | +spring: |
| 143 | + cloud: |
| 144 | + gateway: |
| 145 | + routes: |
| 146 | + - id: remoteaddr_route |
| 147 | + uri: https://example.org |
| 148 | + predicates: |
| 149 | + - RemoteAddr=192.168.1.1/24 |
| 150 | +``` |
| 151 | +## Modifying the way remote addresses are resolved |
| 152 | +> 默认情况下,RemoteAddr 路由谓词工厂使用传入请求中的远程地址。如果 Spring Cloud Gateway 位于代理层后面,这可能与实际的客户端 IP 地址不匹配。 |
| 153 | +您可以通过设置自定义来自定义远程地址的解析方式RemoteAddressResolver。Spring Cloud Gateway 附带一个非默认远程地址解析器,该解析器基于X-Forwarded-For 标头, XForwardedRemoteAddressResolver。 |
| 154 | +XForwardedRemoteAddressResolver有两个静态构造函数方法,它们采用不同的安全方法: |
| 155 | +XForwardedRemoteAddressResolver::trustAll返回 a RemoteAddressResolver,它始终采用标头中找到的第一个 IP 地址X-Forwarded-For。X-Forwarded-For这种方法很容易受到欺骗,因为恶意客户端可以设置解析器接受的初始值。 |
| 156 | +XForwardedRemoteAddressResolver::maxTrustedIndex采用与 Spring Cloud Gateway 前面运行的可信基础设施数量相关的索引。例如,如果 Spring Cloud Gateway 只能通过 HAProxy 访问,则应使用值 1。如果在访问 Spring Cloud Gateway 之前需要可信基础设施的两跳,则应使用值 2。 |
| 157 | +
|
| 158 | +给定下面的header值: |
| 159 | +
|
| 160 | +> X-Forwarded-For: 0.0.0.1, 0.0.0.2, 0.0.0.3 |
| 161 | +
|
| 162 | +使用Java配置: |
| 163 | +
|
| 164 | +```java |
| 165 | +RemoteAddressResolver resolver = XForwardedRemoteAddressResolver |
| 166 | + .maxTrustedIndex(1); |
| 167 | + |
| 168 | +... |
| 169 | + |
| 170 | +.route("direct-route", |
| 171 | + r -> r.remoteAddr("10.1.1.1", "10.10.1.1/24") |
| 172 | + .uri("https://downstream1") |
| 173 | +.route("proxied-route", |
| 174 | + r -> r.remoteAddr(resolver, "10.10.1.1", "10.10.1.1/24") |
| 175 | + .uri("https://downstream2") |
| 176 | +) |
| 177 | +``` |
0 commit comments