1. 程式人生 > >java專案同一瀏覽器下限制使用者重複登入

java專案同一瀏覽器下限制使用者重複登入

此內容有借鑑原文http://blog.csdn.net/huiwenjie168/article/details/7021293

//第一步 


// 此監聽器用來監聽使用者在對session做操作的時候執行相應的方法 

import java.util.*;


import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


import com.wxt.webManager.pojo.User;
 
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener
{
public static Map<String,HttpSession> loginUser = new HashMap<String,HttpSession>();
public static String SESSION_LOGIN_NAME = "user";
User user=new User();
  public void sessionCreated(HttpSessionEvent e)
  {
    HttpSession session = e.getSession();
    session.setMaxInactiveInterval(600);
  }


  public void sessionDestroyed(HttpSessionEvent e)
  {
 try{
 loginUser.remove(e.getSession());
 }catch(Exception et){
 et.printStackTrace();
 } 
  }
  
 public void attributeAdded(HttpSessionBindingEvent e)
 {
if(e.getName().equals(SESSION_LOGIN_NAME)){
loginUser.put(user.getNickname(),e.getSession());
 
}
 }
 
 public void attributeRemoved(HttpSessionBindingEvent e)
 {
if(e.getName().equals(SESSION_LOGIN_NAME)){
try{
System.out.println("session 移除!"+e.getSession());
loginUser.remove(e.getSession());
}catch(Exception et){

}
}
 }
  
 public void attributeReplaced(HttpSessionBindingEvent e)
 {
if(e.getName().equals(SESSION_LOGIN_NAME)){
loginUser.put(user.getNickname(),e.getSession());System.out.println("session 替換!");
}
 }

//判斷使用者是否已經登陸

 public boolean isLogonUser(HttpSession userSession) { 
   Set<String> keys = MySessionListener.loginUser.keySet(); 
   for (String key : keys) { 
       if (loginUser.get(key).equals(userSession)) { 
        System.out.println("該使用者已登入");
           return true; 
       } 
   } 
   System.out.println("該使用者未登入");
   return false; 
}


}

//到的web.xml中去配置listener 

<listener>
<listener-class>com.wxt.webManager.listeners.MySessionListener</listener-class>

</listener>

//第二步 

//在使用者登陸的LoginAction.java,或者是loginServlet.doGet/doPost中 

//驗證使用者名稱、密碼都正確後,再呼叫isLogonUser方法,引數為使用者session;true則表示該使用者已經登陸 

public String execute() throws Exception
  {
  if (this.imgcode.equalsIgnoreCase(ServletActionContext.getRequest() .getSession().getAttribute("rand").toString())) {
      String pwd = new MD5Code().getMD5ofStr(this.user.getPassword());
       Map map = this.userService.isLogin(this.user.getNickname(), pwd);
Boolean bool = new MySessionListener().isLogonUser(ServletActionContext.getRequest().getSession());
System.out.println("狀態:"+bool);
       User u = (User)map.get("user");
       if (u == null) {
        super.addActionError("使用者名稱或密碼錯誤");
        return "input";
       }
if(bool){
super.addActionError("該使用者已登入");
return "input";
}else{
       ServletActionContext.getRequest().getSession().setAttribute("user", u);

}

}
//第三步 

//使用者視窗關閉/或者使用者退出的時候,*一定要   request.getSession().invalidate() 

//使用者視窗關閉js 

          //關閉視窗時呼叫此方法 
          function window.onunload(){ 
              if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey) 
              { 
                    //清除當前session,使用jquery 提供的方法   

                    $.post("${base}/ClearSession.wp"); 

              // [ ${base}/ClearSession.wp ]這是一個請求, 

              //請求到自己寫的ClearSessionServlet 

                  // 在此ClearSessionServlet中重寫doPost方法, 
              // 內容為 request.getSession().invalidate() 

              } 

//寫完此程式碼,就知道如何實現 一個使用者登陸 踢掉之前登陸的使用者了

暫還不能實現不同瀏覽器或不同機器訪問,限值使用者重複登入。 或許可以找些關於騰訊QQ登入限制的文章繼續研究,這個不是很容易做好的。