JAVA注解实现接口防刷

/**
* 测试防刷
*
* @param request
* @return
*/
@ResponseBody
@GetMapping(value = "/testPrevent")
@Prevent //加上该注解即可实现短信防刷(默认一分钟内不允许重复调用,支持扩展、配置)
public Response testPrevent(TestRequest request) {
    return Response.success("调用成功");
}

1.实现防刷切面PreventAop.java

大致逻辑为:定义一切面,通过@Prevent注解作为切入点、在该切面的前置通知获取该方法的所有入参并将其Base64编码,将入参Base64编码+完整方法名作为rediskey,入参作为reidsvalue@Preventvalue作为redis的expire,存入redis;每次进来这个切面根据入参Base64编码+完整方法名判断redis值是否存在,存在则拦截防刷,不存在则允许调用;

1.1 定义注解Prevent

package com.cuonc.aop;

import java.lang.annotation.*;

/**
 * 接口防刷注解
 * 使用:
 * 在相应需要防刷的方法上加上
 * 该注解,即可
 *
 * @author: MoTao
 * @date: 2023/03/19
 */
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Prevent {

    /**
     * 限制的时间值(秒)
     *
     * @return
     */
    String value() default "60";

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

    /**
     * 策略
     *
     * @return
     */
    PreventStrategy strategy() default PreventStrategy.DEFAULT;
}

1.2 实现防刷切面PreventAop

package com.cuonc.aop;

import com.alibaba.fastjson.JSON;
import com.cuonc.common.BusinessException;
import com.cuonc.util.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.Base64;

/**
 * 防刷切面实现类
 *
 * @author: MoTao
 * @date: 2023/03/19
 */
@Aspect
@Component
public class PreventAop {
    private static Logger log = LoggerFactory.getLogger(PreventAop.class);

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 切入点
     */
    @Pointcut("@annotation(com.zetting.aop.Prevent)")
    public void pointcut() {
    }

    /**
     * 处理前
     *
     * @return
     */
    @Before("pointcut()")
    public void joinPoint(JoinPoint joinPoint) throws Exception {
        String requestStr = JSON.toJSONString(joinPoint.getArgs()[0]);
        if (StringUtils.isEmpty(requestStr) || requestStr.equalsIgnoreCase("{}")) {
            throw new BusinessException("[防刷]入参不允许为空");
        }

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = joinPoint.getTarget().getClass().getMethod(methodSignature.getName(),
                methodSignature.getParameterTypes());

        Prevent preventAnnotation = method.getAnnotation(Prevent.class);
        String methodFullName = method.getDeclaringClass().getName() + method.getName();

        entrance(preventAnnotation, requestStr,methodFullName);
        return;
    }

    /**
     * 入口
     *
     * @param prevent
     * @param requestStr
     */
    private void entrance(Prevent prevent, String requestStr,String methodFullName) throws Exception {
        PreventStrategy strategy = prevent.strategy();
        switch (strategy) {
            case DEFAULT:
                defaultHandle(requestStr, prevent,methodFullName);
                break;
            default:
                throw new BusinessException("无效的策略");
        }
    }

    /**
     * 默认处理方式
     *
     * @param requestStr
     * @param prevent
     */
    private void defaultHandle(String requestStr, Prevent prevent,String methodFullName) throws Exception {
        String base64Str = toBase64String(requestStr);
        long expire = Long.parseLong(prevent.value());

        String resp = redisUtil.get(methodFullName+base64Str);
        if (StringUtils.isEmpty(resp)) {
            redisUtil.set(methodFullName+base64Str, requestStr, expire);
        } else {
            String message = !StringUtils.isEmpty(prevent.message()) ? prevent.message() :
                    expire + "秒内不允许重复请求";
            throw new BusinessException(message);
        }
    }

    /**
     * 对象转换为base64字符串
     *
     * @param obj 对象值
     * @return base64字符串
     */
    private String toBase64String(String obj) throws Exception {
        if (StringUtils.isEmpty(obj)) {
            return null;
        }
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] bytes = obj.getBytes("UTF-8");
        return encoder.encodeToString(bytes);
    }
}

注:以上只展示核心代码、其他次要代码(例如redis配置、redis工具类等)可下载源码查阅

2.使用防刷切面

在MyController 使用防刷

package com.cuonc.modules.controller;

import com.cuonc.aop.Prevent;
import com.cuonc.common.Response;
import com.cuonc.modules.dto.TestRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 切面实现入参校验
 */
@RestController
public class MyController {

    /**
     * 测试防刷
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/testPrevent")
    @Prevent
    public Response testPrevent(TestRequest request) {
        return Response.success("调用成功");
    }

    /**
     * 测试防刷
     *
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/testPreventIncludeMessage")
    @Prevent(message = "10秒内不允许重复调多次", value = "10")//value 表示10表示10秒
    public Response testPreventIncludeMessage(TestRequest request) {
        return Response.success("调用成功");
    }
}

原创文章,作者:陌涛,如若转载,请注明出处:https://imotao.com/7329.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
陌涛陌涛
上一篇 2023年3月16日 上午7:19
下一篇 2023年3月19日 下午3:48

相关推荐

  • 手机申请网易音乐人0风险教程分享

    问:怎么申请网易音乐人标识 推荐自己唱 没有版权问题 并且网易云官方是允许翻唱过审核的 自己唱 省钱哈哈哈 这次分享自己唱的教程,比较详细 1.首先需要自己浏览器调成电脑UA访问m…

    2020年4月11日
    52700
  • 一键关闭 Win11广告 OFGB汉化版

    OFGB是一款开源删除 Win11 各处广告的 GUI 工具 它是通过修改 Windows 注册表 来关闭 Windows 11 分散各处的广告设置。软件是用 C# 编写的,界面用…

    2024年11月16日
    2.4K00
  • 国内几家云平台的默认防护阈值

    Ucloud平台与阿里云平台的各个机房有明确数据支撑,但是腾讯云与华为云都是比较模糊的表示是2Gbps,不能保证所有地区的服务器的防护情况都是2Gbps,数据仅供参考。 Uclou…

    2021年3月2日
    27000
  • EPic商城喜加二《怪奇物语3》等限时免费领

    EPic商城喜加二《怪奇物语3》等限时免费领 本周EPic又推出2款游戏限时免费领取分别为:《AER》 与《怪奇物语3》 《AER》是一款冒险探索类游戏 《怪奇物语3》是一款动作冒…

    2020年6月26日
    46700
  • 学生优惠集合

      本文介绍的是利用学生身份可以享受到的相关学生优惠权益,但也希望各位享受权利的同时不要忘记自己的义务,不要售卖、转手自己的学生优惠资格,使得其他同学无法受益。 Gith…

    2019年10月17日
    1.1K00
  • 新一期 阿里云盘免费领取600G容量

    新一期 阿里云盘免费领取600G容量 打开阿里云盘APP,点击左侧 然后点【福利社-兑换福利码】 输入【升职加薪】【人生巅峰】【事业有成】 即可获得3个200G容量,有效期一年!有…

    2021年6月20日
    35000
  • 【棉花云】湖北武汉300M大带宽vps只测不评

    昨天我介绍了棉花云新推出的大带宽套餐,今天就来测一下这款300M的机子表现如何 配置如下 系统信息 多线程测速 IO Geekbench PING 1G文件下载测试

    2024年7月7日
    2.7K00
  • 免费领取英雄联盟15日停机补偿奖励

    免费领取英雄联盟15日停机补偿奖励 英雄联盟在10月15日停机维护了 然后又延长了维护时间 现在补偿玩家一个永久图标 虽然没啥用但是毕竟是永久的不要白不要 另外还有双倍经验卡(3胜…

    2020年10月20日
    67100
  • Windows Terminal 打造自己的高颜值终端

    Windows Terminal是一个面向命令行工具和 shell(如命令提示符、PowerShell 和适用于 Linux 的 Windows 子系统 (WSL))用户的新式终端…

    2020年11月30日
    31900
  • 笔记本/台式机进入 Bios 设置快捷键大全

    组装机主板 品牌笔记本 品牌台式机 主板品牌 启动热键 笔记本品牌 启动热键 台式机品牌 启动热键 华硕主板 F2、Del 或 F12 联想笔记本 F12 联想台式机 F12 技嘉…

    2021年1月19日
    41200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

docker镜像已更新为 https://0-docker.nat.tf/