zoukankan      html  css  js  c++  java
  • 防重复提交

    注解类:

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    /**
    * @Description:
    * @Auther: CW
    * @Date: 2021/8/25 10:24
    */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AvoidRepeatableCommit {
    /**
    * 指定时间内不可重复提交,单位毫秒
    * @return
    */
    long timeout() default 2 ;
    }

    逻辑类:

    import com.cw.common.annotation.AvoidRepeatableCommit;
    import com.cw.common.util.IPUtil;
    import com.cw.common.util.JsonUtil;
    import com.cw.common.util.RedisUtil;
    import com.cw.common.util.ResultData;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;

    import javax.servlet.http.HttpServletRequest;
    import java.lang.reflect.Method;

    /**
    * @Description:
    * @Auther: CW
    * @Date: 2021/8/25 10:24
    */
    @Aspect
    @Component
    public class AvoidRepeatableCommitAspect {
    @Autowired
    private RedisUtil redisUtil;

    @Around("@annotation(com.cw.common.annotation.AvoidRepeatableCommit)")
    public ResultData around(ProceedingJoinPoint point) throws Throwable {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    String ip = IPUtil.getIPAddress(request);
    //获取注解
    MethodSignature signature = (MethodSignature) point.getSignature();
    Method method = signature.getMethod();

    //2.最关键的一步:通过这获取到方法的所有参数名称的字符串数组
    Object[] args = point.getArgs();
    Object o = JsonUtil.objectToJson(args[0]);
    // int i = o.toString().hashCode();

    //目标类、方法
    String className = method.getDeclaringClass().getName();
    String name = method.getName();
    String ipKey = String.format("%s#%s",className,name);
    int hashCode = Math.abs(ipKey.hashCode());
    String key = String.format("%s_%d",ip,hashCode);
    AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class);
    long timeout = avoidRepeatableCommit.timeout();
    if (timeout < 0){
    //过期时间2s
    timeout = 2;
    }
    Integer value = (Integer) redisUtil.get(key);
    if (value!=null && o.toString().hashCode()==value){
    return ResultData.returnData(3,"请勿重复提交",null);
    }
    redisUtil.set(key, o.toString().hashCode(),timeout);

    //执行方法
    Object object = point.proceed();
    if (object instanceof ResultData){
    return (ResultData) object;
    }
    return ResultData.returnData(1,"ok",object);
    }

    }

    调用:

    @AvoidRepeatableCommit
    
    


  • 相关阅读:
    C#动态方法调用
    递归查询SQL语句
    Jquery获取下拉选择节点名称值赋给textbox文本框 获取 父节点的栏目名称编号
    C# 获取指定目录下所有文件信息
    引用借以记录借鉴 实现记住密码和自动登录功能
    摘用的一个实现在线发送短信接口功能
    Winform文件夹图片批量压缩整理修改
    安装包部署项目简述
    一道不错的考试题
    利用ItextSharp 生成PDF文档改进版
  • 原文地址:https://www.cnblogs.com/cw828/p/15246341.html
Copyright © 2011-2022 走看看