tapestry中如何禁止一个用户或是一台机器重复登陆

阅读更多
/** 判断该判断同一个用户是否同时在两台机子上登陆:begin
*/
//根据用户登录名或ID来判断
//轮询hashtable中的信息
HttpSession session =cycle.getRequestContext().getSession();    Enumeration v_enum= CacheData.ht.keys();
while(v_enum.hasMoreElements())
{
String key=(String)v_enum.nextElement();
HttpSession v_session=(HttpSession)CacheData.ht.get(key);
if(v_session==null)
{
CacheData.ht.remove(key);
continue;
}
String v_temp = new String();
try
{
if(v_session.getId()==null)
{ continue;
}
else
{
v_temp=v_session.getAttribute("loginname").toString();
}
}
catch(Exception e)
{
}
if(v_temp.equals(this.getUsername()))
{
//已经存在了,则后替代前
try
{
if(v_session!=session)
{
v_session.invalidate();
CacheData.ht.remove(key);
}
}
catch(Exception e)
{
}
}
}
try
{
session.setAttribute("loginname",this.getUsername());
String aaa = (String)session.getAttribute("loginname");
CacheData.ht.put(session.getId(),session);
}
catch(Exception e)
{
e.printStackTrace();
}



/************
CacheData.java

package com.ce.security.util;
import java.util.Hashtable;
import java.util.Iterator;
import javax.servlet.http.HttpSession;
public class CacheData
{   
    public CacheData()
    {       
    }
    public static Hashtable ht = new Hashtable();
    static public Iterator getSet()
    {
        return ht.values().iterator();
    }
    static public HttpSession getSession(String sessionId)
    {
        return (HttpSession)ht.get(sessionId);
    }
}
*************/ [/color][color=indigo][color=blue][/color]

你可能感兴趣的:(Tapestry,Security,Servlet)