java利用Redis后端接口避免重复提交限制--利用Spring AOP

首先创建自定义标签

package com.common.annotation.AvoidRepeatableCommit;
import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface  AvoidRepeatableCommit {
    /**
     * 指定时间内不可重复提交,单位秒
     * 使用 请求打@AvoidRepeatableCommit(timeout = 10)  时间默认可设置
     */
    int timeout()  default 10 ;
}

使用redis 来做表单防重复提交


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.annotation.AvoidRepeatableCommit;
import com.common.config.Global;
import com.common.persistence.ReturnJson;
import com.common.utils.AddressUtil;
import com.common.utils.JedisUtils;
import com.common.utils.StringUtils;
import com.common.utils.TokenUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * 防重复提交aop
 *
 * @author: admin
 * @Date: 2021/4/13 14:16
 * @Description: 防重复提交
 */

@Aspect
@Component
public class AvoidRepeatableCommitAspect {
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * @param point
     */
    @Around("@annotation(com.common.annotation.AvoidRepeatableCommit)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String ip = AddressUtil.getLocalIp(request);
        //获取注解
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        //获取请求参数
        String param = TokenUtils.getJSONString(request);
        try {
            JSONObject obj = JSON.parseObject(param);
            obj.remove("token");
            obj.remove("timestamp");
            param = obj.toString();
        } catch (Exception e) {
            param = null;
        }
        //目标类、方法
        String className = method.getDeclaringClass().getName();
        String name = method.getName();
        String ipKey = String.format("%s#%s#%s#%s", ip, className, name, param);
        int hashCode = Math.abs(ipKey.hashCode());
        String key = String.format("%s_%d", ip, hashCode);

        logger.info("ipKey={},hashCode={},key={}", ipKey, hashCode, key);

        AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class);
        int timeout = avoidRepeatableCommit.timeout();
        if (timeout < 0) {
            timeout = 10;//默认10 秒
        }
        String value = JedisUtils.get(key);
        if (!StringUtils.isBlank(value)) {
            //由于前端统一返回参数,故返回信息
            return "请不要重复点击!";
        }
        JedisUtils.setex(Global.PC_AVOID_REPEATABLE_COMMIT + key, timeout, "1");
        //执行方法*/
        return point.proceed();
    }
}

你可能感兴趣的:(java利用Redis后端接口避免重复提交限制--利用Spring AOP)