?? 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;
/**
*
* <p>
* Title:用戶入侵檢測信息
* </p>
* <p>
* Description:用于判斷用戶刷新情況檢查,默認為10秒鐘之內連續連接10次為超時
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
* <p>
* Company: www.easyjf.com
* </p>
*
* @author 蔡世友
* @version 1.0
*/
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 < num) {
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()) < maxFailureInterval) {
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;
}
public static void test() {
System.out.println("開始運行!");
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
UserConnectManage.checkLoginValidate("127.0.0." + j, "csy" + j);
}
try {
Thread.sleep(1000);// 暫停1秒
// return;
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("結束運行!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -