使用AOP实现防止表单重复提交问题

在最近的工作开发中,需要设计到防止表单重复提交的问题。在前端的js中做了点击置灰的效果,但是无法防止使用postmain等刷新页面进行恶意攻击 ,就想用后端的技术来完成防止表单提交于是在网上找了一下。特此记录一下自己写的过程与碰到的问题。
借鉴了某位大神的博客后写的忘记保存地址了。。。。。

首先定义一个注解类。

/**
 * 可以防止表单提交的注解
 */
@Retention(RetentionPolicy.RUNTIME)//指定该注解在运行时可以获取
@Target(ElementType.METHOD)//该注解执行当前注解可以作用的范围
public @interface AvoidRepeatableCommit {
    /**
     * 指定时间内不可重复提交,单位毫秒 default 3000默认为三秒
     */
    long timeout()  default 5000 ;
}

定义一个切面类

package com.eccom.cloudnet.audit.aspect;

import com.eccom.cloudnet.audit.annotation.AvoidRepeatableCommit;
import com.eccom.cloudnet.common.dto.Result;
import com.eccom.cloudnet.common.utils.IPUtils;
import com.eccom.cloudnet.common.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
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.concurrent.TimeUnit;

@Aspect
@Component
@Slf4j
public class AvoidRepeatableCommitAspect{

    @Autowired
    private RedisTemplate redisTemplate;

    @Around("@annotation(com.eccom.cloudnet.audit.annotation.AvoidRepeatableCommit)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String ip = IPUtils.getIpAddr(request);//获取客户端请求的ip地址
        log.debug("客户端ip地址:" + ip);
        //获取到注解信息
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        //获取目标类,方法
        String name = method.getName();//获取方法名
        String className = method.getDeclaringClass().getName();//获取类名称
        String ipKey = className + "#" +name;
        log.debug("类名与方法名的hashCode值:"+ipKey.hashCode());
        int keyHashCode = Math.abs(ipKey.hashCode());
        String key = "ECCOM:arc-" + ip +"-"+ keyHashCode;
        log.info("ipKey={},keyHashCode={},key={}",ipKey,keyHashCode,key);
        AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class);
        long timeout = avoidRepeatableCommit.timeout();
        if (timeout < 0 ){
            timeout = 3 * 1000L;
        }
        String value = (String) redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(value)){
            return Result.builder().msg("请勿重复提交!").code(Result.FAILED_CODE).build();
        }
        redisTemplate.opsForValue().set(key, UUIDUtil.generate(),timeout, TimeUnit.MILLISECONDS);
        Object proceed = point.proceed();
        return proceed;
    }
}

在我们需要实现防止表单提交的接口上添加@VoidRepeatableCommit注解

	@AvoidRepeatableCommit
    @PostMapping(value = "/test")
    public Result test(){
        return testService.test(commitLabelDto);
    }

注意事项

如果在redis中获取到了值时我们会return回去一个提示信息,这里在return时一定会出现类转换异常问题。通常如果我们项目中没有定义规范的返回类型。我们这里可以选择全局异常来捕获。如果有定义规范的返回类型,我们这里直接返回对应的信息。否则会出现类转换异常

String value = (String) redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(value)){
            return Result.builder().msg("请勿重复提交!").code(Result.FAILED_CODE).build();
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值