1. 序言
除了自定义过滤器,官方也开放了断言扩展,可以自定义断言器
2. 实现
2.1. java代码
实现接口AbstractRoutePredicateFactory
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
| @Component @Slf4j public class TenantRoutePredicateFactory extends AbstractRoutePredicateFactory<TenantRoutePredicateFactory.Config> {
public TenantRoutePredicateFactory() { super(Config.class); }
@Override public Predicate<ServerWebExchange> apply(Config config) {
return new GatewayPredicate() { @SneakyThrows @Override public boolean test(ServerWebExchange serverWebExchange) { List<String> tenant = serverWebExchange.getRequest().getHeaders().get("tenant"); return tenant !=null && config.tenantId.equals(tenant.get(0)); } }; } public static class Config { private String tenantId;
public String getName() { return tenantId; }
public void setName(String name) { this.tenantId = name; } } }
|
注意命名规则以RoutePredicateFactory
结尾,配置文件使用的时候才识别
2.2. 配置文件
1 2 3 4 5 6 7 8 9 10 11
| routes: - id: provider-predicate uri: lb://CloudGatewayProvider predicates: - name: Tenant args: tenantId: hello
|
不知为啥,简写形式上面内部类Config
读出来没有数据