密码相关处理

本文介绍了一种登录保护机制,该机制通过限制每60秒内连续失败登录次数为5次,并在10分钟后清除缓存记录,以防止网络攻击。详细解析了登录失败后的处理流程及账号锁定解除条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、60秒以内5次失败就不让登录,报 ErrorDesc.NETWORK_ATTACT错误;超过10分钟再次登录的话 以前的缓存就会被清理掉;正常只有失败的登录记录才会被缓存记录

 private static Map<String,int[]> loginMap = new ConcurrentHashMap<>();

 

  //登录后的校验    

    if(!safeCheck(jsonObj.getString("accountname"))){
            return ErrorDesc.getResultStr(command, ErrorDesc.NETWORK_ATTACT);
        }
        

            //登录后失败的处理
            if(loginMap.containsKey(accountName)){
                int[] tempRecord = loginMap.get(accountName);
                int tmpCount= tempRecord[0];
                int timeStamp = tempRecord[1];
                int timeSum = tempRecord[2];
                int currentTime = (int)(System.currentTimeMillis()/1000);
                timeSum = timeSum + (currentTime - timeStamp);
                tmpCount ++;
                tempRecord[0] = tmpCount;
                tempRecord[1] = currentTime;
                tempRecord[2] = timeSum;
                loginMap.replace(accountName, tempRecord);
                LogUtil.hsLogDebug(String.format("202================= %s, %s,%s,%s", tmpCount,timeStamp,timeSum,currentTime));
            }else{
                int tmpCount= 1;
                int timeStamp = (int) (System.currentTimeMillis()/1000);
                int[] tempRecord = new int[3];
                tempRecord[0] = tmpCount;
                tempRecord[1] = timeStamp;
                tempRecord[2] = 0;
                loginMap.put(accountName, tempRecord);
                LogUtil.hsLogDebug(String.format("211================= %s, %s", tmpCount,timeStamp));
            }
 

 

private static boolean safeCheck(String accountName){
        
        if(loginMap.containsKey(accountName)){
            
            int[] tempRecord = loginMap.get(accountName);
            int tmpCount= tempRecord[0];
            int timeStamp = tempRecord[1];
            int timeSum = tempRecord[2];
            int timeDiff = (int) (System.currentTimeMillis()/1000) - timeStamp;
            LogUtil.hsLogDebug(String.format("419================= %s, %s,%s", tmpCount,timeSum,timeDiff));
            //时间差超过十分钟释放账号锁定
            if(timeDiff > 10 * 60){
                loginMap.remove(accountName);
                return true;
            }else if(tmpCount > 5){
                if(timeSum <=60){
                    return false;
                }else{
                    loginMap.remove(accountName);
                    return true;
                }
            }
        }
        return true;
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值