如何为JavaWeb应用程序增加入侵检测功能
来源:优易学  2011-3-24 11:48:25   【优易学:中国教育考试门户网】   资料下载   IT书店

 

  三、监控线程UserConnectManage.java类
  这是入侵检测的核心部分,主要实现具体的入侵检测、记录、判断用户信息、在线用户的刷新等功能,并提供其它应用程序使用本组件的调用接口。
  package com.easyjf.web;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  import org.apache.log4j.Logger;
  /**
  *
  *
  Title:用户入侵检测信息
  *
  Description:用于判断用户刷新情况检查,默认为10秒钟之内连续连接10次为超时
  public class UserConnectManage {
  private static final Logger logger = (Logger) Logger.getLogger(UserConnectManage.class.getName());
  private static int maxFailureTimes=10;//最大登录失败次数
  private static long maxFailureInterval=10000;//毫秒,达到最大登录次数且在这个时间范围内
  private static long waitInterval=60000;//失败后接受连接的等待时间,默认1分钟
  private static int maxOnlineUser=200;//同时在线的最大数
  private final static Map users=new HashMap();//使用ip+userName为key存放用户登录信息UserLoginAuth
  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] [2] [3] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训