路由断言Factories
Spring Cloud Gateway 是路由使用spring webflux的Handler Mapping为基础结构实现的。
Spring Cloud Gateway包含许多内置的路由断言Factories。这些断言都匹配HTTP请求的不同属性。多个路由断言Factories可以通过 and 组合使用
默认的一些路由断言 Factory配置
#路由断言
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
/**
* @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<</

本文介绍了Spring Cloud Gateway的路由断言Factories,包括默认的路由断言配置,并详细阐述了如何创建和配置自定义的RoutePredicateFactory,以及通过属性文件和代码方式进行配置的方法。
最低0.47元/天 解锁文章
3076

被折叠的 条评论
为什么被折叠?



