防重复提交

private ResultMsg stopSameForm(HttpServletRequest request,
		String user,
		String urlPath,String requestBody){
	try{
		
		urlPath=urlPath.toLowerCase();
		if(!(urlPath.contains("/save") 
				|| urlPath.contains("/add")
				|| urlPath.contains("/create")
				|| urlPath.contains("/new")
				|| urlPath.contains("/update")
				)){
			return new ResultMsg(ResultCode.Success);
		}
		String ip=StringUtils.getIpAddr(request);
		
		String lockKey=com.lujie.userauth.constant.KeysManager.User_Auth+"FormLock"+user+MD5Util.encrypt(requestBody);
		String lockIPKey=lockKey+ip;
		
		String lockIP=jedisTool.get(lockIPKey);
		if(lockIP!=null && !lockIP.equals("null")){
			return new ResultMsg("730","高频访问接口,请稍后再进行操作!");
		}
		Integer expireSecond=Integer.parseInt(UserAuthConfig.AccessLockTime.toString())/1000;
		
		if(jedisTool.getLock(lockKey, System.currentTimeMillis()+"")==0){
			String lockTime=jedisTool.get(lockKey);
			Long accessMinintervalTime=UserAuthConfig.AccessMinintervalTime;//每次访问接口间隔最短时间
			if (lockTime!=null && System.currentTimeMillis() < Long.parseLong(lockTime)+accessMinintervalTime) {
				log.info("ip:"+ip+","+urlPath);
				jedisTool.resetLock(lockKey, System.currentTimeMillis()+"");
				
				jedisTool.getLock(lockIPKey, System.currentTimeMillis()+"");
				//String bbb=jedisTool.get(lockIPKey);
				
				jedisTool.expire(lockIPKey, expireSecond);//60秒过期
				return new ResultMsg("720","高频访问接口,请稍后再进行操作!");
			}
			
			
			if (lockTime!=null && System.currentTimeMillis() < Long.parseLong(lockTime)+UserAuthConfig.AccessLockTime) {
				return new ResultMsg("700","重复相同的请求,请稍后再进行操作!");
			}else{
				jedisTool.unLock(lockKey);
				return new ResultMsg(ResultCode.Success);
			}
		}
		jedisTool.expire(lockKey, expireSecond);//自动解锁时间
	}catch(Exception lockException){
		log.info("urlPath:"+urlPath+", requestBody:"+ requestBody);
		log.info(lockException.getMessage());
		return new ResultMsg("710","服务器表单数据锁异常");
	}
	return new ResultMsg(ResultCode.Success); 
}
posted @   yifanSJ  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示