防止表单重复提交注解类

注解类

import java.lang.annotation.*;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface NoRepeatSubmit {

    /**
     * Key前缀
     */
    String key() default "";

    /**
     * 拼接方式
     */
    String split() default "_";

    /**
     * 参数是否拼接
     */
    boolean params() default false;

    /**
     * 拼接的参数
     */
    String[] splitParams() default {};
}

拦截器:

import com.alibaba.fastjson.JSON;
import com.yuelvhui.interfaces.NoRepeatSubmit;
import com.yuelvhui.util.exception.ServiceException;
import com.yuelvhui.utils.Constants;
import com.yuelvhui.utils.ErrorCode;
import com.yuelvhui.utils.redis.RedisService;
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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.concurrent.TimeUnit;

@Aspect
@Configuration
public class SubmitAspect {

    private static final Logger logger = LoggerFactory.getLogger(SubmitAspect.class);

    @Autowired
    private RedisService redisService;

    @Around("execution(public * *(..)) && @annotation(com.yuelvhui.interfaces.NoRepeatSubmit)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String key = "";
        if (Constants.CONTENT_TYPE_JSON.equals(request.getContentType())) {
            Object[] parameterValues = pjp.getArgs();
            key = getJsonKey(signature, parameterValues, request);
        } else {
            Method method = signature.getMethod();
            key = getKey(method, request);
        }
        if (!redisService.exists(key)) {
            try {
                redisService.put(key, request.getSession().getId(), 2, TimeUnit.SECONDS);
                return pjp.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                throw new ServiceException(ErrorCode.ServiceException.getCode(), throwable.getMessage());
            }
        } else {
            logger.info("请勿重复提交");
            throw new ServiceException(ErrorCode.DataRepetition.getCode(), ErrorCode.DataRepetition.getMessage());
        }
    }

    private String getJsonKey(MethodSignature signature, Object[] parameterValues, HttpServletRequest request) {
        Method method = signature.getMethod();
        NoRepeatSubmit noRepeatSubmit = method.getAnnotation(NoRepeatSubmit.class);
        String key = noRepeatSubmit.key();
        String name = method.getName();
        boolean params = noRepeatSubmit.params();
        String split = noRepeatSubmit.split();
        key = key + split + name;
        if (params) {
            String[] parameterNames = signature.getParameterNames();
            for (int i = 0; i < parameterValues.length; i++) {
                try {
                    String s = JSON.toJSONString(parameterValues[i]);
                    key = key + split + parameterNames[i] +split + s;
                } catch (Exception e) {
                    continue;
                }
            }
        }
        return key;
    }


    private String getKey(Method method, HttpServletRequest request) {
        NoRepeatSubmit noRepeatSubmit = method.getAnnotation(NoRepeatSubmit.class);
        String key = noRepeatSubmit.key();
        String name = method.getName();
        boolean params = noRepeatSubmit.params();
        String split = noRepeatSubmit.split();
        key = key + split + name;
        if (params) {
            String[] splitParams = noRepeatSubmit.splitParams();
            if (splitParams != null && splitParams.length > 0) {
                for (String splitParam : splitParams) {
                    key = key + split + request.getParameter(splitParam);
                }
            } else {
                Enumeration enu = request.getParameterNames();
                while (enu.hasMoreElements()) {
                    String paraName = (String) enu.nextElement();
                    key = key + split + request.getParameter(paraName);
                }
            }
        }
        return key;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值