Spring-Cloud-Gateway Predicate断言使用与自定义

路由断言Factories

Spring Cloud Gateway 是路由使用spring webfluxHandler Mapping为基础结构实现的。
Spring Cloud Gateway包含许多内置的路由断言Factories。这些断言都匹配HTTP请求的不同属性。多个路由断言Factories可以通过 and 组合使用

默认的一些路由断言 Factory配置

1
2
3
4
5
6
7
8
#路由断言
spring.cloud.gateway.routes[0].predicates[0]=Path=/auth/**
#After路由断言,使用一个日期时间,在该日期以后请求才被匹配
spring.cloud.gateway.routes[0].predicates[1]=After=2019-09-12T17:42:47.789-07:00
#Before路由断言,使用一个日期时间,在该日期之前才被匹配
spring.cloud.gateway.routes[0].predicates[1]=Before=2019-09-12T17:42:47.789-07:00
#Between路由断言,使用两个参数用逗号分隔,在两个时间范围内的请求才被匹配
spring.cloud.gateway.routes[0].predicates[1]=Between=2019-09-12T17:42:47.789-07:00,2019-10-12T17:42:47.789-07:00

除了这些还有许多内置的路由断言Factories可以去官网的文档查看。

自定义路由Predicate 断言

spring-cloud-gateway的官方文档中没有给出自定义Predicate ,只留下一句TODO: document writing Custom Route Predicate Factories

创建RoutePredicateFactory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

/**
* @author WXY
*/
@Slf4j
public class TokenRoutePredicateFactory extends AbstractRoutePredicateFactory<TokenRoutePredicateFactory.Config> {


private static final String DATETIME_KEY = "headerName";

public TokenRoutePredicateFactory() {
super(Config.class);
}

@Override
public List<String> shortcutFieldOrder() {
return Collections.singletonList(DATETIME_KEY);
}

@Override
public Predicate<ServerWebExchange> apply(Config config) {
log.debug("TokenRoutePredicateFactory Start...");
return exchange -> {
//判断header里有放token
HttpHeaders headers = exchange.getRequest().getHeaders();
List<String> header = headers.get(config.getHeaderName());
log.info("Token Predicate headers:{}", header);
return header.size() > 0;
};
}

public static class Config {
/**
* 传输token header key
*/
private String headerName;

public String getHeaderName() {
return headerName;
}

public void setHeaderName(String headerName) {
this.headerName = headerName;
}
}
}

继承AbstractRoutePredicateFactory<C>主要实现其中的两个方法

shortcutFieldOrder()-Config对应的字段

Predicate<ServerWebExchange> apply(Config config)-具体的逻辑

还有就是构造方法传入用来装配置的类程序会自动把配置的value传入apply中的入参

初始化RoutePredicateFactory为bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author WXY
*
*/
@Configuration
public class RoutesConfiguration {

@Bean
public TokenRoutePredicateFactory initTokenRoutePredicateFactory(){
return new TokenRoutePredicateFactory();
}

}

或者直接在TokenRoutePredicateFactory类上加@Component也行

配置自定义的Predicate

使用属性文件配置自定义Predicate
1
spring.cloud.gateway.routes[1].predicates[1]=Token=Authorization

其中Toekn为命名RoutePredicateFactory时的前面部分,所以在定义RoutePredicateFactory时类名必须后缀为RoutePredicateFactory,否则找不到自定义的Predicate

使用代码配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @author WXY
*
*/
@Configuration
public class RoutesConfiguration {
/**
* 代码配置路由
*/
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes().route(predicateSpec ->
predicateSpec.path("/order/**")
.and().asyncPredicate(initTokenRoutePredicateFactory().applyAsync(config -> config.setHeaderName("Authorization")))
.uri("lb://order-service").id("order-service")

).build();
}

@Bean
public TokenRoutePredicateFactory initTokenRoutePredicateFactory(){
return new TokenRoutePredicateFactory();
}

}

使用代码配置自定义Predicate,主要使用asyncPredicate方法,把所需的自定义RoutePredicateFactory对象传进去配置applyAsync方法传入配置的属性。

spring-cloud-gateway中文文档

示例代码