springboot+redis 防止表单重复提交 非token验证


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * @description: 防止表单重复提交
 * @author: 云晓得峰
 **/
@Target(ElementType.METHOD) // 作用到方法上
@Retention(RetentionPolicy.RUNTIME) // 运行时有效
public @interface NoRepeatSubmit {
    /**
     * 指定时间内不可重复提交,单位毫秒
     */
    long timeout()  default 3000 ;
}

```java

import com.yckt.game.utils.IpUtils;
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.data.redis.core.StringRedisTemplate;
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;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: 重复提交aop
 * @author: 云晓得峰
 **/
@Aspect
@Component
public class NoRepeatSubmitAop {
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * @param point
     */
    @Around("@annotation(com.yckt.game.utils.NoRepeatSubmit.NoRepeatSubmit)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        HttpServletRequest request  = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String ip = IpUtils.getIpAddr(request);
        //获取注解
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        //目标类、方法
        String className = method.getDeclaringClass().getName();
        String name = method.getName();
        String ipKey = String.format("%s#%s",className,name);
        String requestParam = getAllRequestParam(request);
        int hashCode = Math.abs(ipKey.hashCode());

        NoRepeatSubmit avoidRepeatableCommit =  method.getAnnotation(NoRepeatSubmit.class);
        long timeout = avoidRepeatableCommit.timeout();
        Long increment = redisTemplate.opsForValue().increment(requestParam+"_"+ip+hashCode, 1);
        if (increment==1){
            redisTemplate.expire(requestParam+"_"+ip+hashCode, timeout, TimeUnit.MILLISECONDS);
        }else {
//            返回公共响应体  根据项目自行修改
            return LayuiPageInfo.createPageInfoError(0001,"重复提交!");
        }
        Object object = point.proceed();

        return object;
    }
    private String getAllRequestParam(HttpServletRequest request) {
        Map<String, String> res = new HashMap<String, String>();
        StringBuffer sb = new StringBuffer();
        Enumeration<?> temp = request.getParameterNames();
        if (null != temp) {
            while (temp.hasMoreElements()) {
                String en = (String) temp.nextElement();
                String value = request.getParameter(en);
                sb.append(en+"="+value+"|");

            }
        }
        return sb.toString();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白菜S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值