自定义断言

本文介绍了如何在Spring Cloud Gateway中创建一个自定义断言工厂,用于根据用户年龄进行路由过滤。通过配置文件设置年龄范围,并在代码中实现断言逻辑,确保只有年龄在18到60之间的请求才能通过网关访问特定服务。

使用方法

修改配置文件

server:
  port: 8000

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: shop-product
          uri: lb://shop-product
          order: 0
          predicates:
           - Path=/product/**
           - Age=18,60
  application:
    name: my-gateway



2.创建自定义断言的类

package com.xp.geteway;

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.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;

import javax.validation.constraints.NotNull;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @program: spring-cloud
 * @description: 这是一个自定义断言
 *
 * @author: 许鹏
 * @create: 2021-07-08 15:38
 **/
@Component
public class AgeRoutePredicateFactory extends AbstractRoutePredicateFactory<AgeRoutePredicateFactory.Config> {
    public static final String DATETIME1_KEY = "datetime1";
    public static final String DATETIME2_KEY = "datetime2";

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

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

    @Override
    public Predicate<ServerWebExchange> apply(AgeRoutePredicateFactory.Config config) {
        return new GatewayPredicate() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                ServerHttpRequest request = serverWebExchange.getRequest();
                String age=request.getQueryParams().getFirst("age");
                if (StringUtils.isEmpty(age)){
                    return false;
                }else {
                    Integer a=Integer.parseInt(age);
                    if (a>=config.getMinAge()&&a<=config.getMaxAge()){
                        return true;
                    }else {
                        return false;
                    }
                }
            }


        };
    }



    @Validated
    public static class Config {
        @NotNull
        private Integer minAge;
        @NotNull
        private Integer maxAge;

        public Integer getMinAge() {
            return minAge;
        }

        public void setMinAge(Integer minAge) {
            this.minAge = minAge;
        }

        public Integer getMaxAge() {
            return maxAge;
        }

        public void setMaxAge(Integer maxAge) {
            this.maxAge = maxAge;
        }
    }
}

3.启动后观察 年龄大于18小于60的能访问,其余的不可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值