防重复提交

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 @ 2023-10-27 08:28  yifanSJ  阅读(95)  评论(0编辑  收藏  举报