@Aspect
@Component
public class RepeatSubmitAspect {
@Autowired
private RedisUtils redisUtils;
@Around("execution(public * io.xz.modules..*(..)) && @annotation(io.xz.annotation.Login) && @annotation(io.xz.annotation.RepeatSubmitAnnotation)")
public Object around(ProceedingJoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Object [] objArgs = joinPoint.getArgs();
String key=null;
SysUserEntity sysUserEntity = null;
RepeatSubmitAnnotation repeatSubmitAnnotation=method.getAnnotation(RepeatSubmitAnnotation.class);
if(null != objArgs && objArgs.length >0){
for (Object objArg : objArgs) {
if (objArg instanceof SysUserEntity) {
sysUserEntity = (SysUserEntity) objArg;
break;
}
}
}
if(null != sysUserEntity){
key = getCacheKey(repeatSubmitAnnotation.value(),sysUserEntity.getUserId());
if (StringUtils.isNotBlank(key)) {
if(StringUtils.isNotBlank( redisUtils.get(key))){
throw new RRException("请勿重复提交");
}
// 如果是第一次请求,就将key存入缓存中
redisUtils.set(key, key,5);
}
}
try {
return joinPoint.proceed();
} catch (Throwable throwable) {
throw new RRException("服务器异常");
} finally {
if(StringUtils.isNotBlank(key)) {
redisUtils.delete(key);
}
}
}
/**
* 将来还要加上用户的唯一标识
*/
private String getCacheKey(String name,Long userId) {
return isOSLinux()+userId+name;
}
public static String isOSLinux() {
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
if (os != null && os.toLowerCase().indexOf("linux") > -1) {
return "linux";
} else {
return "windows";
}
}
}
重复提交
最新推荐文章于 2023-05-17 19:00:48 发布