Spring Cloud Gateway 2 断言
2021-05-13 06:28
标签:orm 设置 java配置 匹配 method 错误信息 bean spring 路径 Spring Cloud Gateway提供了很多断言设置,当http请求进入Spring Cloud Gateway时,网关中的路由断言工厂会根基配置的路由规则,对http请求进行断言匹配,匹配成功的请求进行路由转发,失败的直接返回是错误信息。 以下是常用的断言工厂 设定一个UTC时间,此时间之后的请求会成功,此时间之前的请求会404 UTC 时间生成: java配置 yml配置 设定一个时间,此时间之前的请求会成功,此时间之后的请求会404 设定一个时间区间,此时间区间内的请求会成功 设置两个参数:cookie的key和value,当请求中携带一条此信息时,请求成功; 配置多个值: 设置两个参数:header的key和value,当请求携带匹配信息时,请求成功 设定一个主机名,当请求信息来自此主机时,请求成功; 指定请求的方法:如POST,GET 指定路径下的请求,进行转发 指定一个请求参数的key,符合条件的进行转发 如:http://localhost:8080/hello?key=value 中的key 指定ip或ip段,符合条件的转发成功 Spring Cloud Gateway 2 断言 标签:orm 设置 java配置 匹配 method 错误信息 bean spring 路径 原文地址:https://www.cnblogs.com/chenglc/p/13131904.htmlSpring Cloud Gateway 断言
After
//比当前时间早一小时的时间
String minTime = ZonedDateTime.now().minusHours(1).format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(minTime);
@Bean
public RouteLocator customRoteLocator(RouteLocatorBuilder builder) {
//生成一个比当前时间早一个小时的UTC时间
ZonedDateTime minusTime = LocalDateTime.now().minusHours(1).atZone(ZoneId.systemDefault());
return builder.routes()
.route("after_route", predicateSpec -> predicateSpec.after(minusTime)
.uri("http://baidu.com"))
.build();
}
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
Before
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
Between
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
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
pring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
Header
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=key,value
Host
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
Method
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
Path
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
Query
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=key
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
RemoteAddr
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24