gateway routes规则
1、After Route
所有匹配的请求必须在这个时间点之后
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2022-05-23T19:52:33.736+08:00[Asia/Shanghai] #时间格式为ZonedDateTime.now()
2、Before Route
所有匹配的请求必须在这个时间点之前
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2021-05-23T19:52:33.736+08:00[Asia/Shanghai]
3、Between Route
所有匹配的请求必须在这两个时间点之内
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2022-05-23T19:52:33.736+08:00[Asia/Shanghai], 2023-05-23T19:52:33.736+08:00[Asia/Shanghai] #两个时间点之间不能用空格隔开
4、Cookie Route
接收Cookie参数,且值需要与正则表达是匹配
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
5、Header Route
在请求Header中,需要带有X-Request-Id的参数,且值需要和正则表达式匹配
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
6、Host Route
接收一个参数,主机名模式,判断请求的host是否满足匹配规则
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
7、Method Route
接收一个参数Method,表示请求的方式匹配参数
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
8、Path Route
接收一个路径集合,表示请求的路径需要匹配Path中的路径
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
9、Query Route
接收两个参数,请求param和正则表达式,判断请求参数是否具有给定名称且值与正则表达式
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
10、RemoteAddr Route
路由谓词工厂采用的RemoteAddr
列表(最小大小为 1)sources
,它们是 CIDR 表示法(IPv4 或 IPv6)字符串,例如192.168.0.1/16
(其中192.168.0.1
是 IP 地址和16
子网掩码)。以下示例配置 RemoteAddr 路由谓词:
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
11、 Weight Route
接收一组【组名,权重】,然后对于同一个组内的路由按照权重转发
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
12、 XForwardedRemoteAddr Route
路由谓词工厂采用的XForwarded Remote Addr
列表(最小大小为 1)sources
,它们是 CIDR 表示法(IPv4 或 IPv6)字符串,例如192.168.0.1/16
(其中192.168.0.1
是 IP 地址和16
子网掩码)。
此路由谓词允许根据X-Forwarded-For
HTTP 标头过滤请求。
这可以与反向代理一起使用,例如负载平衡器或 Web 应用程序防火墙,其中仅当请求来自这些反向代理使用的受信任的 IP 地址列表时才允许请求。
spring:
cloud:
gateway:
routes:
- id: xforwarded_remoteaddr_route
uri: https://example.org
predicates:
- XForwardedRemoteAddr=192.168.1.1/24
11、自定义路由断言工厂
1、必须是Spring的Bean
2、类必须以RoutePredicateFactory结尾(约定大于配置)
3、必须继承AbstractRoutePredicateFactory
4、必须声明静态内部类 声明属性来接收 配置文件中对应的断言的信息
5、需要结合shortcutFieldOrder进行绑定
6、通过apply进行逻辑判断 true就是匹配成功 false匹配失败
package com.wwh.blog.config;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;
import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
@Component
public class TokenRoutePredicateFactory extends AbstractRoutePredicateFactory<TokenRoutePredicateFactory.Config> {
public TokenRoutePredicateFactory() {
super(Config.class);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
return new GatewayPredicate() {
@Override
public boolean test(ServerWebExchange exchange) {
String token = config.getToken();
if (Objects.isNull(token) && !token.equals("auth")) {
return false;
}
// there is a value and since regexp is empty, we only check existence.
return true;
}
@Override
public Object getConfig() {
return config;
}
@Override
public String toString() {
return String.format("Token: %s ", config.token);
}
};
}
@Override
public List<String> shortcutFieldOrder() {
return Collections.singletonList("token");
}
@Validated
public static class Config {
@NotEmpty
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Config(String token) {
this.token = token;
}
}
}