/** * 防止重复提交 */ @Aspect @Component public class AvoidRepeatableCommitAspect { @Autowired private RedisTemplate redisTemplate; /** * @param point */ @Around("@annotation(com.xwolf.boot.annotation.AvoidRepeatableCommit)") public Object around(ProceedingJoinPoint point) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest(); String ip = IPUtil.getIP(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); int hashCode = Math.abs(ipKey.hashCode()); String key = String.format("%s_%d",ip,hashCode); log.info("ipKey={},hashCode={},key={}",ipKey,hashCode,key); AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class); long timeout = avoidRepeatableCommit.timeout(); if (timeout < 0){ //过期时间5分钟 timeout = 60*5; } String value = (String) redisTemplate.opsForValue().get(key); if (StringUtils.isNotBlank(value)){ return "请勿重复提交"; } redisTemplate.opsForValue().set(key, UUIDUtil.uuid(),timeout,TimeUnit.MILLISECONDS); //执行方法 Object object = point.proceed(); return object; } }