spring cloud gateway常用的断言,以及分别代码示例
时间: 2023-07-30 12:12:05 浏览: 78
Spring Cloud Gateway 常用的断言有以下几种:
1. `after`:判断请求是否在指定时间之后
示例代码:
```java
gatewayFilterChain.filter(exchange).then(Mono.fromRunnable(() -> {
ZonedDateTime dateTime = ZonedDateTime.parse("2022-01-01T00:00:00+08:00");
assertThat(exchange.getRequest().getHeaders().getDate()).isAfter(dateTime);
}));
```
2. `before`:判断请求是否在指定时间之前
示例代码:
```java
gatewayFilterChain.filter(exchange).then(Mono.fromRunnable(() -> {
ZonedDateTime dateTime = ZonedDateTime.parse("2022-01-01T00:00:00+08:00");
assertThat(exchange.getRequest().getHeaders().getDate()).isBefore(dateTime);
}));
```
3. `cookie`:判断请求是否包含指定名称和值的 Cookie
示例代码:
```java
gatewayFilterChain.filter(exchange).then(Mono.fromRunnable(() -> {
assertThat(exchange.getRequest().getCookies().get("name").getValue()).isEqualTo("value");
}));
```
4. `header`:判断请求是否包含指定名称和值的 Header
示例代码:
```java
gatewayFilterChain.filter(exchange).then(Mono.fromRunnable(() -> {
assertThat(exchange.getRequest().getHeaders().get("name")).isEqualTo("value");
}));
```
5. `host`:判断请求是否包含指定 Host 值
示例代码:
```java
gatewayFilterChain.filter(exchange).then(Mono.fromRunnable(() -> {
assertThat(exchange.getRequest().getHeaders().getHost().getHostName()).isEqualTo("example.com");
}));
```
阅读全文