private static Thread checkThread=null;
private static class CheckTimeOut implements Runnable{
private Thread parentThread;
public CheckTimeOut(Thread parentThread)
{
this.parentThread=parentThread;
synchronized(this){
if(checkThread==null){
checkThread= new Thread(this);
//System.out.println("创建一个新线程!");
checkThread.start();
}
}
}
public void run() {
while(true)
{
if(parentThread.isAlive()){
try{
Thread.sleep(2000);
int i=0;
if(users.size()>maxOnlineUser)//当达到最大用户数时清除
{
synchronized(users){//执行删除操作
Iterator it=users.keySet().iterator();
Set set=new HashSet();
Date now=new Date();
while(it.hasNext())
{
Object key=it.next();
UserConnect user=(UserConnect)users.get(key);
if(now.getTime()-user.getFirstFailureTime().getTime()>maxFailureInterval)//删除超时的用户
{
set.add(key);
logger.info("删除了一个超时的连接"+i);
i++;
}
}
if(i<5)//如果删除少于5个,则强行删除1/2在线记录,牺牲性能的情况下保证内存
{
int num=maxOnlineUser/2;
it=users.keySet().iterator();
while(it.hasNext() && i {
set.add(it.next());
logger.info("删除了一个多余的连接"+i);
i++;
}
}
users.keySet().removeAll(set);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
break;
}
}
logger.info("监视程序运行结束!");
}
}
//通过checkLoginValidate判断是否合法的登录连接,如果合法则继续,非法则执行
public static boolean checkLoginValidate(String ip,String userName)//只检查认证失败次数
{
boolean ret=true;
Date now=new Date();
String key=ip+":"+userName;
UserConnect auth=(UserConnect)users.get(key);
if(auth==null)//把用户当前的访问信息加入到users容器中
{
auth=new UserConnect();
auth.setIp(ip);
auth.setUserName(userName);
auth.setFailureTimes(0);
auth.setFirstFailureTime(now);
users.put(key,auth);
if(checkThread==null)new CheckTimeOut(Thread.currentThread());
}
else
{
if(auth.getFailureTimes()>maxFailureTimes)
{
//如果在限定的时间间隔内,则返回拒绝用户连接的信息
if((now.getTime()-auth.getFirstFailureTime().getTime()) {
ret=false;
auth.setStatus(-1);
}
else if(auth.getStatus()==-1 && (now.getTime()-auth.getFirstFailureTime().getTime()<(maxFailureInterval+waitInterval)))//重置计数器
{
ret=false;
}
else
{
auth.setFailureTimes(0);
auth.setFirstFailureTime(now);
auth.setStatus(0);
}
}
//登录次数加1
auth.setFailureTimes(auth.getFailureTimes()+1);
}
//System.out.println(key+":"+auth.getFailureTimes()+":"+ret+":"+(now.getTime()-auth.getFirstFailureTime().getTime()));
return ret;
}
public static void reset(String ip,String userName)//重置用户信息
{
Date now=new Date();
String key=ip+":"+userName;
UserConnect auth=(UserConnect)users.get(key);
if(auth==null)//把用户当前的访问信息加入到users容器中
{
auth=new UserConnect();
auth.setIp(ip);
auth.setUserName(userName);
auth.setFailureTimes(0);
auth.setFirstFailureTime(now);
users.put(key,auth);
}
else
{
auth.setFailureTimes(0);
auth.setFirstFailureTime(now);
}
}
public static void remove(String ip,String userName)//删除用户在容器中的记录
{
String key=ip+":"+userName;
users.remove(key);
}
public static void clear()//清空容器中内容
{
if(!users.isEmpty())users.clear();
}
public static long getMaxFailureInterval() {
return maxFailureInterval;
}
public static void setMaxFailureInterval(long maxFailureInterval) {
UserConnectManage.maxFailureInterval = maxFailureInterval;
}
public static int getMaxFailureTimes() {
return maxFailureTimes;
}
public static void setMaxFailureTimes(int maxFailureTimes) {
UserConnectManage.maxFailureTimes = maxFailureTimes;
}
public static int getMaxOnlineUser() {
return maxOnlineUser;
}
public static void setMaxOnlineUser(int maxOnlineUser) {
UserConnectManage.maxOnlineUser = maxOnlineUser;
}
public static long getWaitInterval() {
return waitInterval;
}
public static void setWaitInterval(long waitInterval) {
UserConnectManage.waitInterval = waitInterval;
}
四、调用接口
在需要进入侵检测判断的地方,直接使用UserConnectManage类中的checkLoginValidate方法即可。如EasyJWeb的核心Servlet
com.easyjf.web.ActionServlet中调用UserConnectManage的代码:
if(!UserConnectManage.checkLoginValidate(request.getRemoteAddr(),"guest"))
{
info(request,response,new Exception("您对页面的刷新太快,请等待"+UserConnectManage.getWaitInterval()/1000+"秒
后再刷新页面!"));
return;
}
五、总结
当然,这里提供的方法只是一个简单的实现示例,由于上面的用户信息是直接保存在内存中,若并发用户很大的时候的代码的占用,可以考虑引入数据库来记录用户的访问信息,当然相应的执行效率肯定用降低。上面介绍的实现中,入侵检测判断的策略也只有用户访问次数及时间间隔两个元素,您还可以根据你的实现情况增加其它的检测元素。
阅读推荐
相关文章