赞
踩
package com.dechnic.oms.common.annotation;
import java.lang.annotation.*;
/**
* @desc 自定义表单重复提交
* @author houqida
* @date 2023/2/27
*/
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit
{
/**
* 间隔时间(ms),小于此时间视为重复提交
*/
public int interval() default 3000;
/**
* 防重复操作限时标记数值(存储redis限时标记数值)
*/
String value() default "value" ;
/**
* 提示消息
*/
public String message() default "不允许重复提交,请稍候再试";
}
package com.dechnic.oms.framework.aspectj;
import com.dechnic.oms.common.annotation.RepeatSubmit;
import com.dechnic.oms.common.constant.CacheConstants;
import com.dechnic.oms.common.constant.ExceptionEnum;
import com.dechnic.oms.common.core.redis.RedisCache;
import com.dechnic.oms.common.exception.ServiceException;
import com.dechnic.oms.common.utils.MapperUtil;
import com.dechnic.oms.framework.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
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;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* @ desc:
* @ Author: houqida
* @ Date : 2023/2/27
*/
@Slf4j
@Component
@Aspect
public class NoRepeatSubmitAspect {
@Autowired
private RedisCache redisCache;
@Autowired
private TokenService tokenService;
@Pointcut("@annotation(com.dechnic.oms.common.annotation.RepeatSubmit)")
public void preventDuplication(){
};
@Around("preventDuplication()")
public Object around(ProceedingJoinPoint joinPoint) throws Exception{
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 执行方法
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
// 获取重复提交注解
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
// 获取token 标记及方法标记,生成redisKey 和redisValue
String token = tokenService.getToken(request);
String url = request.getRequestURI();
// 通过前缀 + url + token + 函数参数签名 来生成redis上的 key
String redisKey = CacheConstants.REPEAT_SUBMIT_KEY
.concat(url)
.concat(token)
.concat(getMethodSign(method,joinPoint.getArgs()));
// 这个值只是为了标记,不重要
String redisValue = redisKey.concat(annotation.value()).concat("submit duplication");
if (!redisCache.hasKey(redisKey)){
redisCache.setCacheObject(redisKey,redisValue,annotation.interval(), TimeUnit.MILLISECONDS);
try {
return joinPoint.proceed();
} catch (Throwable throwable) {
//确保方法执行异常实时释放限时标记(异常后置通知)
redisCache.deleteObject(redisKey);
throw new ServiceException(throwable.getMessage());
}
}else {
// 重复提交了抛出异常,如果是在项目中,根据具体情况处理。
throw new ServiceException(ExceptionEnum.REPEAT_SUBMIT.getError(),ExceptionEnum.REPEAT_SUBMIT.getCode());
}
}
/**
* 生成方法标记:采用数字签名算法SHA1对方法签名字符串加签
*
* @param method
* @param args
* @return
*/
private String getMethodSign(Method method, Object... args) {
StringBuilder sb = new StringBuilder(method.toString());
for (Object arg : args) {
sb.append(toString(arg));
}
return DigestUtils.sha1Hex(sb.toString());
}
private String toString(Object arg) {
if (Objects.isNull(arg)) {
return "null";
}
if (arg instanceof Number) {
return arg.toString();
}
return MapperUtil.objectToJson(arg);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。