切面拦截aop实现接口访问次数的限制

目标实现1分钟内同一个接口同一个ip只能访问300次,超次数之后需冷却60秒,60秒之后才允许访问。(key:  LIMIT_METHOD_NAME+IP+方法名)

1. 自定义一个注解,标记访问次数和时间限制等参数

/**
 * 接口防刷注解
 */
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Limition {
    /**
     * 限制的时间值(秒)默认60s
     */
    long value() default 60;

    /**
     * 限制规定时间内访问次数,默认只能访问一次
     */
    long times() default 1;

    /**
     * 提示
     */
    String message() default "";

    /**
     * 策略
     */
    LimitStrategy strategy() default LimitStrategy.DEFAULT;
    
}
/**
 * 防刷策略枚举
 */
public enum LimitStrategy {
    /**
     * 默认(60s内不允许再次请求)
     */
    DEFAULT
}

2. 定义一个切面拦截方法

/**
 * 防刷切面实现类
 */
@Aspect
@Component
@Slf4j
public class LimitAop {
    @Resource
    private RedisTemplate<String, Long> redisTemplate;
    private final String  LIMIT_METHOD_NAME = "LIMIT_METHOD_NAME";
    /**
     * 切入点
     */
    @Pointcut("@annotation(XXX.XXX.XXX.utils.Limition)")
    public void pointcut() {}
    /**
     * 处理前
     */
    @Before("pointcut()")
    public void joinPoint(JoinPoint joinPoint) throws Exception {
        // 获取调用者ip
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        String userIP = IpUtils.getIpAddr(httpServletRequest);
        // 获取调用接口方法名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = joinPoint.getTarget().getClass().getMethod(
                methodSignature.getName(),
                methodSignature.getParameterTypes()); // 获取该接口方法
        String methodFullName = method.getDeclaringClass().getName() + method.getName(); // 获取到方法名
        Limition limitionAnnotation = method.getAnnotation(Limition.class); // 获取该接口上的prevent注解(为了使用该注解内的参数)
        // 执行对应策略
        entrance(limitionAnnotation, userIP, methodFullName);
    }
    /**
     * 通过prevent注册判断执行策略
     * @param limition 该接口的prevent注解对象
     * @param userIP 访问该接口的用户ip
     * @param methodFullName 该接口方法名
     */
    private void entrance(Limition limition, String userIP, String methodFullName) throws Exception {
        LimitStrategy strategy = limition.strategy(); // 获取校验策略
        if (Objects.requireNonNull(strategy) == LimitStrategy.DEFAULT) { // 默认就是default策略,执行default策略方法
            defaultHandle(userIP, limition, methodFullName);
        } else {
            throw new ServiceException("无效的策略");
        }
    }
    /**
     * Default测试执行方法
     * @param userIP 访问该接口的用户ip
     * @param limition 该接口的prevent注解对象
     * @param methodFullName 该接口方法名
     */
    private void defaultHandle(String userIP, Limition limition, String methodFullName) throws Exception {
        String base64StrIP = toBase64String(userIP); // 加密用户ip(避免ip存在一些特殊字符作为redis的key不合法)
        long expire = limition.value(); // 获取访问限制时间
        long times = limition.times(); // 获取访问限制次数
        // 限制特定时间内访问特定次数
        long count = redisTemplate.opsForValue().increment(LIMIT_METHOD_NAME + base64StrIP + ":" + methodFullName, 1); // 访问次数+1
        if (count == 1) { // 如果访问次数为1,则重置访问限制时间(即redis超时时间)
            redisTemplate.expire(
                    LIMIT_METHOD_NAME + base64StrIP + ":" + methodFullName,
                    expire,
                    TimeUnit.SECONDS);
        }
        if (count > times) { // 如果访问次数超出访问限制次数,则禁止访问
            // 如果有限制信息则使用限制信息,没有则使用默认限制信息
            log.info("------------ip{}访问接口{}.太频繁--------------", userIP , methodFullName);
            String errorMessage =
                    !StringUtils.isEmpty(limition.message()) ? limition.message() : expire + "秒内不允许重复请求";
            throw new ServiceException(errorMessage);
        }
    }
    /**
     * 对象转换为base64字符串
     * @param obj 对象值
     * @return base64字符串
     */
    private String toBase64String(String obj) {
        if (StringUtils.isEmpty(obj)) {
            return null;
        }
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] bytes = obj.getBytes(StandardCharsets.UTF_8);
        return encoder.encodeToString(bytes);
    }
    
    
    
}

3. 使用(在需要限制的controller 方法上加上@Limition注解, 后面的时间和限制次数可以自定义)

  /**
     * 查询用户信息
     * @param userCode
     * @return
     */
    @GetMapping("/queryUserInfo")
    @ApiOperation("查询用户信息")
    @Limition(value = 60 ,times = 300, message = "访问频繁,请休息一下吧!")
    public AjaxResult queryUserInfo(@ApiParam(name = "用户编码编号",value = "userCode") String userCode){
        //查询用户信息
        
        return AjaxResult.success();
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值