ºìÁªLinuxÃÅ»§
Linux°ïÖú

Java WebÖÐÈëÇÖ¼ì²â¼°¼òµ¥ÊµÏÖ

·¢²¼Ê±¼ä:2006-11-09 00:45:12À´Ô´:ºìÁª×÷Õß:coolbg
¡¡¡¡ÔÚJava WebÓ¦ÓóÌÖУ¬ÌرðÊÇÍøÕ¾¿ª·¢ÖУ¬ÎÒÃÇÓÐʱºòÐèҪΪӦÓóÌÐòÔö¼ÓÒ»¸öÈëÇÖ¼ì²â³ÌÐòÀ´·ÀÖ¹¶ñÒâË¢ÐµĹ¦ÄÜ£¬·ÀÖ¹·Ç·¨Óû§²»¶ÏµÄÍùWebÓ¦ÓÃÖÐÖظ´·¢ËÍÊý¾Ý¡£µ±È»£¬ÈëÇÖ¼ì²â¿ÉÒÔÓúܶ෽·¨ÊµÏÖ£¬°üÀ¨Èí¼þ¡¢Ó²¼þ·À»ðǽ£¬ÈëÇÖ¼ì²âµÄ²ßÂÔÒ²ºÜ¶à¡£ÔÚÕâÀïÎÒÃÇÖ÷Òª½éÉܵÄÊÇJava WebÓ¦ÓóÌÐòÖÐͨ¹ýÈí¼þµÄ·½Ê½ÊµÏÖ¼òµ¥µÄÈëÇÖ¼ì²â¼°·ÀÓù¡£

¡¡¡¡¸Ã·½·¨µÄʵÏÖÔ­ÀíºÜ¼òµ¥£¬¾ÍÊÇÓû§·ÃÎÊWebϵͳʱ¼Ç¼ÿ¸öÓû§µÄÐÅÏ¢£¬È»ºó½øÐжÔÕÕ£¬²¢¸ù¾ÝÉ趨µÄ²ßÂÔ(±ÈÈ磺1ÃëÖÓË¢ÐÂÒ³Ãæ10´Î)ÅжÏÓû§ÊÇ·ñÊôÓÚ¶ñÒâˢС£

¡¡¡¡ÎÒÃǵÄÈëÇÖ¼ì²â³ÌÐòÓ¦¸Ã·Åµ½ËùÓÐJava Web³ÌÐòµÄÖ´ÐÐÇ°£¬Ò²¼´Èô·¢ÏÖÓû§ÊǶñÒâˢоͲ»ÔÙ¼ÌÐøÖ´ÐÐJava WebÖеÄÆäËü²¿·ÖÄÚÈÝ£¬·ñÔò¾Í»áʧȥÁËÒâÒå¡£Õâ¾ÍÐèÒªÒÔ²å¼þµÄ·½Ê½°ÑÈëÇÖ¼ì²âµÄ³ÌÐòÖÃÈëJava WebÓ¦ÓÃÖУ¬Ê¹µÃÿ´ÎÓû§·ÃÎÊJava Web£¬¶¼ÏÈÒªµ½Õâ¸öÈëÇÖ¼ì²â³ÌÐòÖб¨Ò»´Îµ½£¬·ûºÏ¹æÔò²ÅÄÜ·ÅÐС£

¡¡¡¡Java WebÓ¦ÓôóÖ·ÖΪÁ½ÖÖ£¬Ò»ÖÖ´¿JSP(+Java Bean)·½Ê½£¬Ò»ÖÖÊÇ»ùÓÚ¿ò¼Ü(ÈçStruts¡¢EasyJWebµÈ)µÄ¡£µÚÒ»ÖÖ·½Ê½µÄJava Web¿ÉÒÔͨ¹ýJava ServletÖеÄFilter½Ó¿ÚʵÏÖ£¬Ò²¼´ÊµÏÖÒ»¸öFilter½Ó¿Ú£¬ÔÚÆädoFilter·½·¨ÖвåÈëÈëÇÖ¼ì²â³ÌÐò£¬È»ºóÔÙweb.xmlÖÐ×÷¼òµ¥µÄÅäÖü´¿É¡£ÔÚ»ùÓÚ¿ò¼ÜµÄWebÓ¦ÓÃÖУ¬ÓÉÓÚËùÓÐÓ¦Óö¼ÓÐÒ»¸öÈë¿Ú£¬Òò´Ë¿ÉÒÔ°ÑÈëÇÖ¼ì²âµÄ³ÌÐòÖ±½Ó²åÈë¿ò¼ÜÈë¿ÚÒýÇæÖУ¬Ê¹¿ò¼Ü±¾ÉíÖ§³ÖÈëÇÖ¼ì²â¹¦ÄÜ¡£µ±È»,Ò²¿ÉÒÔͨ¹ýʵÏÖFilter½Ó¿ÚÀ´ÊµÏÖ¡£

¡¡¡¡ÔÚEasyJWeb¿ò¼ÜÖУ¬ÒѾ­ÖÃÈëÁ˼òµ¥ÈëÇÖ¼ì²âµÄ³ÌÐò£¬Òò´Ë£¬ÕâÀïÎÒÃÇÒÔEasyJWeb¿ò¼ÜΪÀý,½éÉܾßÌåµÄʵÏÖ·½·¨¼°Ô´Â룬ÍêÕûµÄ´úÂë¿ÉÒÔÔÚEasyJWebÔ´ÂëÖÐÕÒµ½¡£

¡¡¡¡ÔÚ»ùÓÚEasyJWebµÄJava WebÓ¦ÓÃÖУ¬Ä¬ÈÏÇé¿öÏÂÄãÖ»ÒªÁ¬ÐøË¢ÐÂÒ³Ãæ´ÎÊý¹ý¶à£¬¼´»áµ¯³öÈçϵĴíÎó£º

¡¡¡¡EasyJWeb¿ò¼ÜÓÑÇéÌáʾ!:-):
¡¡¡¡Äú¶ÔÒ³ÃæµÄË¢ÐÂÌ«¿ì,ÇëµÈ´ý60ÃëºóÔÙË¢ÐÂÒ³Ã棡

¡¡¡¡¶þ¡¢Óû§·ÃÎÊÐÅÏ¢¼Ç¼UserConnect.javaÀà¡¡¡¡

¡¡¡¡Õâ¸öÀàÊÇÒ»¸ö¼òµ¥µÄJava Bean£¬Ö÷Òª´ú±íÓû§µÄÐÅÏ¢£¬°üÀ¨Óû§Ãû¡¢IP¡¢µÚÒ»´Î·ÃÎÊʱ¼ä¡¢×îºóµÇ¼ʱ¼ä¡¢µÇ¼´ÎÊý¡¢Óû§×´Ì¬µÈ¡£È«²¿

´úÂëÈçÏ£º

package com.easyjf.web;

import java.util.Date;
/**
*
*

Title:Óû§ÑéÖ¤ÐÅÏ¢


*
Description:¼Ç¼Óû§µÇ¼ÐÅÏ¢,ÅжÏÓû§µÇ¼Çé¿ö


*
Copyright: Copyright (c) 2006


*
Company: www.easyjf.com


* @author ²ÌÊÀÓÑ
* @version 1.0
*/
public class UserConnect {
private String userName;
private String ip;
private Date firstFailureTime;
private Date lastLoginTime;
private int failureTimes;//Óû§µÇ¼ʧ°Ü´ÎÊý
private int status=0;//Óû§×´Ì¬0±íʾÕý³£,-1±íʾËø¶¨
public int getFailureTimes() {
return failureTimes;
}
public void setFailureTimes(int failureTimes) {
this.failureTimes = failureTimes;
}
public Date getFirstFailureTime() {
return firstFailureTime;
}

public void setFirstFailureTime(Date firstFailureTime) {
this.firstFailureTime = firstFailureTime;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public Date getLastLoginTime() {
return lastLoginTime;
}

public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

}
Èý¡¢¼à¿ØÏß³ÌUserConnectManage.javaÀà

¡¡¡¡ÕâÊÇÈëÇÖ¼ì²âµÄºËÐIJ¿·Ö£¬Ö÷ҪʵÏÖ¾ßÌåµÄÈëÇÖ¼ì²â¡¢¼Ç¼¡¢ÅжÏÓû§ÐÅÏ¢¡¢ÔÚÏßÓû§µÄˢеȹ¦ÄÜ£¬²¢ÌṩÆäËüÓ¦ÓóÌÐòʹÓñ¾×é¼þµÄµ÷Óýӿڡ£

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´ÎΪ³¬Ê±


*
Copyright: Copyright (c) 2006


*
Company: www.easyjf.com


* @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 {
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;
} ¡¡
¡¡¡¡¡¡¡¡
¡¡¡¡Îå¡¢×ܽá

¡¡¡¡µ±È»£¬ÕâÀïÌṩµÄ·½·¨Ö»ÊÇÒ»¸ö¼òµ¥µÄʵÏÖʾÀý£¬ÓÉÓÚÉÏÃæµÄÓû§ÐÅÏ¢ÊÇÖ±½Ó±£´æÔÚÄÚ´æÖУ¬Èô²¢·¢Óû§ºÜ´óµÄʱºòµÄ´úÂëµÄÕ¼Ó㬿ÉÒÔ¿¼ÂÇÒýÈëÊý¾Ý¿âÀ´¼Ç¼Óû§µÄ·ÃÎÊÐÅÏ¢£¬µ±È»ÏàÓ¦µÄÖ´ÐÐЧÂʿ϶¨ÓýµµÍ¡£ÉÏÃæ½éÉܵÄʵÏÖÖУ¬ÈëÇÖ¼ì²âÅжϵIJßÂÔÒ²Ö»ÓÐÓû§·ÃÎÊ´ÎÊý¼°Ê±¼ä¼ä¸ôÁ½¸öÔªËØ£¬Äú»¹¿ÉÒÔ¸ù¾ÝÄãµÄʵÏÖÇé¿öÔö¼ÓÆäËüµÄ¼ì²âÔªËØ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ