gateway断言配置详解

一、Predicate - 断⾔

1、简单用法

XML 复制代码
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

2、自定义断言

新建类VipRoutePredicateFactory,注意VipRoutePredicateFactory名字的前缀和yml文件的配置必须一致,即yml文件必须配置为前缀Vip

java 复制代码
import jakarta.validation.constraints.NotEmpty;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;

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


@Component
public class VipRoutePredicateFactory extends AbstractRoutePredicateFactory<VipRoutePredicateFactory.Config> {


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

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return new GatewayPredicate() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                // localhost/search?q=haha&user=leifengyang
                ServerHttpRequest request = serverWebExchange.getRequest();

                String first = request.getQueryParams().getFirst(config.param);

                return StringUtils.hasText(first) && first.equals(config.value);
            }
        };
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("param", "value");
    }

    /**
     * 可以配置的参数
     */
    @Validated
    public static class Config {

        @NotEmpty
        private String param;


        @NotEmpty
        private String value;

        public @NotEmpty String getParam() {
            return param;
        }

        public void setParam(@NotEmpty String param) {
            this.param = param;
        }

        public @NotEmpty String getValue() {
            return value;
        }

        public void setValue(@NotEmpty String value) {
            this.value = value;
        }
    }
}

yml文件配置:

XML 复制代码
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
        - Vip=user,wangcj  #表示传了参数user为wangcj的才满足要求
相关推荐
nvd111 天前
企业级全场景 API 网关实践:基于 Kong Hybrid 模式的跨 VPC 部署与 GitOps 治理
gateway·kong
nvd111 天前
深度解析:Kong Hybrid 模式与 KIC (Gateway API) 架构演进与核心异同
架构·gateway·kong
zx2859634001 天前
Laravel10.x重磅升级:核心特性全解析
mysql·gateway·智能路由器
暗夜猎手-大魔王2 天前
转载--AI Agent 架构设计:Gateway 架构设计(OpenClaw、Claude Code、Hermes Agent 对比)
gateway
SarL EMEN2 天前
Gateway Timeout504 网关超时的完美解决方法
gateway
2601_949194263 天前
Gateway Timeout504 网关超时的完美解决方法
gateway
码点滴4 天前
私有 Gateway 接入企业 IM:从消息路由到多租户隔离——Hermes Agent 工程实战
人工智能·架构·gateway·prompt·智能体·hermes
代码写到35岁4 天前
Gateway+OpenFeign 踩坑总结
gateway
invicinble4 天前
对于gateway信息量沉淀
gateway
郝开5 天前
Spring Cloud Gateway 3.5.14 使用手册
java·数据库·spring boot·gateway