一
在Java Web應用程中
該方法的實現原理很簡單
我們的入侵檢測程序應該放到所有Java Web程序的執行前
Java Web應用大致分為兩種
在EasyJWeb框架中
在基於EasyJWeb的Java Web應用中
EasyJWeb框架友情提示!
您對頁面的刷新太快
二
這個類是一個簡單的Java Bean
代碼如下
package com
import java
/**
*
*
Title:用戶驗證信息
*
Description:記錄用戶登錄信息
*
Copyright: Copyright (c)
*
Company:
* @author 蔡世友
* @version
*/
public class UserConnect {
private String userName;
private String ip;
private Date firstFailureTime;
private Date lastLoginTime;
private int failureTimes;//用戶登錄失敗次數
private int status=
public int getFailureTimes() {
return failureTimes;
}
public void setFailureTimes(int failureTimes) {
this
}
public Date getFirstFailureTime() {
return firstFailureTime;
}
public void setFirstFailureTime(Date firstFailureTime) {
this
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this
}
public Date getLastLoginTime() {
return lastLoginTime;
}
public void setLastLoginTime(Date lastLoginTime) {
this
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this
}
}
三
這是入侵檢測的核心部分
package com
import java
import java
import java
import java
import java
import java
import org
/**
*
*
Title:用戶入侵檢測信息
*
Description:用於判斷用戶刷新情況檢查
*
Copyright: Copyright (c)
*
Company:
* @author 蔡世友
* @version
*/
public class UserConnectManage {
private static final Logger logger = (Logger) Logger
private static int maxFailureTimes=
private static long maxFailureInterval=
private static long waitInterval=
private static int maxOnlineUser=
private final static Map users=new HashMap()
private static Thread checkThread=null;
private static class CheckTimeOut implements Runnable{
private Thread parentThread;
public CheckTimeOut(Thread parentThread)
{
this
synchronized(this){
if(checkThread==null){
checkThread= new Thread(this)
//System
checkThread
}
}
}
public void run() {
while(true)
{
if(parentThread
try{
Thread
int i=
if(users
{
synchronized(users){//執行刪除操作
Iterator it=users
Set set=new HashSet()
Date now=new Date()
while(it
{
Object key=it
UserConnect user=(UserConnect)users
if(now
{
set
(
i++;
}
}
if(i<
{
int num=maxOnlineUser/
it=users
while(it
set
(
i++;
}
}
users
}
}
}
catch(Exception e)
{
e
}
}
else
{
break;
}
}
(
}
}
//通過checkLoginValidate判斷是否合法的登錄連接
public static boolean checkLoginValidate(String ip
{
boolean ret=true;
Date now=new Date()
String key=ip+
UserConnect auth=(UserConnect)users
if(auth==null)//把用戶當前的訪問信息加入到users容器中
{
auth=new UserConnect()
auth
auth
auth
auth
users
if(checkThread==null)new CheckTimeOut(Thread
}
else
{
if(auth
{
//如果在限定的時間間隔內
if((now
ret=false;
auth
}
else if(auth
{
ret=false;
}
else
{
auth
auth
auth
}
}
//登錄次數加
auth
}
//System
return ret;
}
public static void reset(String ip
{
Date now=new Date()
String key=ip+
UserConnect auth=(UserConnect)users
if(auth==null)//把用戶當前的訪問信息加入到users容器中
{
auth=new UserConnect()
auth
auth
auth
auth
users
}
else
{
auth
auth
}
}
public static void remove(String ip
{
String key=ip+
users
}
public static void clear()//清空容器中內容
{
if(!users
}
public static long getMaxFailureInterval() {
return maxFailureInterval;
}
public static void setMaxFailureInterval(long maxFailureInterval) {
UserConnectManage
}
public static int getMaxFailureTimes() {
return maxFailureTimes;
}
public static void setMaxFailureTimes(int maxFailureTimes) {
UserConnectManage
}
public static int getMaxOnlineUser() {
return maxOnlineUser;
}
public static void setMaxOnlineUser(int maxOnlineUser) {
UserConnectManage
}
public static long getWaitInterval() {
return waitInterval;
}
public static void setWaitInterval(long waitInterval) {
UserConnectManage
}
四
在需要進入侵檢測判斷的地方
com
if(!UserConnectManage
{
info(request
後再刷新頁面!
return;
}
五
當然
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27150.html