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;
}