81,092
社区成员
发帖
与我相关
我的任务
分享
public isBanLogin(){
//最大出错3次
int errorCount=3;
//periodCache ip对应的时间段Cache
Map<String,Date> periodCache=new HashMap<String,Date>();
//登陆ip缓存
Map<String,Integer> ipCache=new HashMap<String,Integer>();
//如果此ip在已经在30分钟内被禁止了
if(periodCache.get(ip)!=null){
if(当前日期-periodCache.get(ip)<30分钟){
return true;
}else{
//重设
periodCache.put(ip,null);
ipCache.put(ip,0);
}
}
boolean isBan=false;
String ip=null;
if(ipCache.get(ip)==null){
//第一次登陆失败
ipCache.put(ip,new Integer(1));
isBan=false;
}else{
//登陆失败超过3次则禁止
if(ipCache.get(ip)>errorCount){
isBan=true;
}else{
int temp=ipCache.get(ip);
ipCache.put(ip,new Integer(temp++));
isBan=false;
}
}
if(isBan){
periodCache.put(ip,new Date());
}
return ban;
}