通过SessionID和用户名来保证同一个用户不能同时登录

可以通过SessionID和用户名来保证同一个用户不能同时登录的问题,下面程序模仿了QQ的登录,当登录后判断当前帐号是否已经登录,如果登录。则踢掉以前登录的用户。


重置sessionID

[java]  view plain  copy
  1. private void createNewSession(HttpServletRequest request, HttpServletResponse response, EntCurrentUser entCurrentUser) throws Exception {  
  2.         HttpSession oldSession = request.getSession();  
  3.         Enumeration attrNames = oldSession.getAttributeNames();  
  4.         Map attrMap = new HashMap();  
  5.           
  6.         while(attrNames != null && attrNames.hasMoreElements()) {  
  7.             String attrName = (String)attrNames.nextElement();  
  8.             attrMap.put(attrName, oldSession.getAttribute(attrName));  
  9.         }  
  10.           
  11.         oldSession.invalidate();  
  12.         HttpSession newSession = request.getSession(true);  
  13.           
  14.         Set keySet = attrMap.keySet();  
  15.         if(keySet != null && !keySet.isEmpty()) {  
  16.             Iterator it = keySet.iterator();  
  17.             while(it != null && it.hasNext()) {  
  18.                 String key = (String)it.next();  
  19.                 newSession.setAttribute(key, attrMap.get(key));  
  20.             }  
  21.         }  
  22.         if(entCurrentUser != null) {  
  23.             entCurrentUser.setNewSession(newSession);  
  24.         }  
  25.     }  


参考资料: http://www.xuebuyuan.com/465665.html

参考资料:http://blog.csdn.net/cctcc/article/details/9773185

猜你喜欢

转载自blog.csdn.net/adsadadaddadasda/article/details/79564649