SpringBoot实现表单重复提交检测
前言
在实际开发过程中,web应用经常会出现网络延迟,接口处理时间略长,用户习惯等原因造成的客户连续多次点击提交按钮调用接口,导致数据库会出现重复数据或这接口业务逻辑bug等问题
方案
【SpringBoot实现表单重复提交检测】利用redis锁实同一个用户同一个请求2秒内重复提交返回错误路由
SubmitLock
标记需要拦截的方法
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SubmitLock {
int expire() default 2;
}
RedisLockUtil redis锁校验及写入
@Component
public class RedisLockUtil {
@Autowired
private RedisUtil redisUtil;
private int lockDBIndex = 1;
public boolean lock(String key,String clientID,int lockExpire){
if(redisUtil.isValid(key,lockDBIndex)){
return false;
}else{
redisUtil.redisTemplateSet(key,clientID,lockDBIndex);
redisUtil.setExpire(key,lockExpire, TimeUnit.SECONDS,lockDBIndex);
return true;
}
}
}
RepeatSubmitAspect 统一拦截切面
@Aspect
@Component
@Order(value = https://www.it610.com/article/100)
public class RepeatSubmitAspect {
private static Logger logger = LoggerFactory.getLogger(RepeatSubmitAspect.class);
@Autowired
private RedisLockUtil redisLockUtil;
/**
* 切面点 指定注解
*/
@Pointcut("@annotation(com.haopan.frame.common.annotation.SubmitLock) " +
"|| @within(com.haopan.frame.common.annotation.SubmitLock)")
public void repeatSubmitAspect() {}/**
* 拦截方法指定为 repeatSubmitAspect
*/
@Around("repeatSubmitAspect()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
SubmitLock submitLock = method.getAnnotation(SubmitLock.class);
if (submitLock != null) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String token = request.getHeader("token");
if (!StringUtil.isEmpty(token)) {
String path = request.getServletPath();
String key = "submitLock|" + token + "|" + path;
String clientId = CommonUtil.getNewGuid();
if (redisLockUtil.lock(key, clientId, submitLock.expire())) {
// 获取锁成功
return point.proceed();
} else {
System.out.println("tryLock fail, key = ["+key+"]");
return Result.errorResult().setMsg("重复请求,请稍后再试").setCode(-980);
}
} else {
return point.proceed();
}
} else {
return point.proceed();
}
}
}
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- SpringBoot调用公共模块的自定义注解失效的解决
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- 解决SpringBoot引用别的模块无法注入的问题
- java中如何实现重建二叉树