路由断言工厂(
Route Predicate Factories
)配置
https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories
作用: 当请求gateway的时候, 使用断言对请求进行匹配, 如果匹配成功就路由转发, 如果匹配失败就返回404
类型:内置,自定义
此类型的断言根据时间做判断,主要有三个:
AfterRoutePredicateFactory: 接收一个日期参数,判断请求日期是否晚于指定日期
BeforeRoutePredicateFactory: 接收一个日期参数,判断请求日期是否早于指定日期
BetweenRoutePredicateFactory: 接收两个日期参数,判断请求日期是否在指定时间段内
ZonedDateTime.now()
1
‐
After
=
2019
‐
12
‐
31
T23
:
59
:
59.789
+
08
:
00
[
Asia
/
Shanghai
]
- id: services-protection uri: lb://spcloud-services-protection predicates: - Path=/protection/** - After=2022-04-10T15:27:24.576+08:00[Asia/Shanghai] - Cookie=uuid_tt_dd,\d+ filters: - StripPrefix=1
基于远程地址的断言工厂
RemoteAddrRoutePredicateFactory:接收一个IP地址段,判断请求主机地址是否在地址段中
1
‐
RemoteAddr
=
192.168.1.1
/
24
基于Cookie的断言工厂
CookieRoutePredicateFactory:接收两个参数,cookie 名字和一个正则表达式。 判断请求
cookie是否具有给定名称且值与正则表达式匹配。
1
‐
Cookie
=
chocolate
,
ch
.
实战:
- id: services-protection uri: lb://spcloud-services-protection predicates: - Path=/protection/** - After=2022-04-10T15:27:24.576+08:00[Asia/Shanghai] - Cookie=uuid_tt_dd,\d+ filters: - StripPrefix=1
基于Header的断言工厂
HeaderRoutePredicateFactory:接收两个参数,标题名称和正则表达式。 判断请求Header是否具有给定名称且值与正则表达式匹配。
1
‐
Header
=
X
‐
Request
‐
Id
,
\d
+
基于Host的断言工厂
HostRoutePredicateFactory:接收一个参数,主机名模式。判断请求的Host是否满足匹配规则。
1
‐
Host
=**
.
testhost
.
org
基于Method请求方法的断言工厂
MethodRoutePredicateFactory:接收一个参数,判断请求类型是否跟指定的类型匹配。
1
‐
Method
=
GET
基于Path请求路径的断言工厂
PathRoutePredicateFactory:接收一个参数,判断请求的URI部分是否满足路径规则。
1
‐
Path
=/
foo
/
{
segment
}