javaweb 同名用户登录剔除退出功能 - Go语言中文社区

javaweb 同名用户登录剔除退出功能


前几天,网上找了些朋友的资料,做了一个小功能,验证用户是否重复登录。

原理就是:每一个用户,登录前有一个验证,当第一次登录时,会把其session信息,添加到一个特定的静态变量中。当第二次登录时,验证到静态变量中存在该用户的信息,就表示为重复登录。

jsp代码,一个form表单提交:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <form action="/struts2upload/system/login/reLogin.action"  
  2.             method="post">  
  3.             用户名:  
  4.             <input type="text" id="txtUser" name="txtUser" value="" />  
  5.             密  码:  
  6.             <input type="text" id="txtPass" name="txtPass" value="" />  
  7.             <input type="submit" id="subOk" value="确定" />  
  8.         </form>  
struts2配置:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <action name="reLogin" class="userLoginAction" method="reLogin">  
  2.             <result name="input" type="redirect">/relogin.jsp</result>  
  3.             <result name="success" type="redirect">/ok.jsp</result>  
  4.         </action>  
action代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.      * 可判断用户是否重复登录 
  3.      */  
  4.     public String reLogin() {  
  5.         String userId = this.getTxtUser();//ServletActionContext.getRequest().getParameter("txtUser");  
  6.         UserInfo user = new UserInfo();  
  7.         user.setUserId(userId);  
  8.         //验证该用户ID,是否已经登录。当前用户比较已登录到系统的静态变量中的值,是否存在。  
  9.         Boolean hasLogin = SessionUserListener.checkIfHasLogin(user);  
  10.         // 如果重复登录,控制端则打印信息,返回登录页面  
  11.         if (hasLogin) {  
  12.             System.out.println(user.getUserId()+"已经登录到本系统。");  
  13.             return "input";  
  14.             // SessionUserListener.removeUserSession(userId);  
  15.         } else {  
  16.             // 如果没有重复登录,则将该登录的用户信息添加入session中  
  17.             ServletActionContext.getRequest().getSession().setAttribute("userInfo", user);  
  18.             // 比较保存所有用户session的静态变量中,是否含有当前session的键值映射,如果含有就删除  
  19.             if (SessionUserListener.containsKey(ServletActionContext.getRequest().getSession().getId())) {  
  20.                 SessionUserListener.removeSession(ServletActionContext.getRequest().getSession().getId());  
  21.             }  
  22.             //把当前用户封装的session按,sessionID和session进行键值封装,添加到静态变量map中。  
  23.             SessionUserListener.addUserSession(ServletActionContext.getRequest().getSession());  
  24.         }  
  25.         return "success";  
  26.     }  
session监听类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.zyujie.listener;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.http.HttpSession;  
  10. import javax.servlet.http.HttpSessionEvent;  
  11. import javax.servlet.http.HttpSessionListener;  
  12.   
  13. import com.zyujie.pojo.UserInfo;  
  14.   
  15. public class SessionUserListener implements HttpSessionListener {  
  16.   
  17.     // key为sessionId,value为HttpSession,使用static,定义静态变量,使之程序运行时,一直存在内存中。  
  18.     private static java.util.Map<String, HttpSession> sessionMap = new java.util.concurrent.ConcurrentHashMap<String, HttpSession>(500);  
  19.   
  20.     /** 
  21.      * HttpSessionListener中的方法,在创建session 
  22.      */  
  23.     public void sessionCreated(HttpSessionEvent event) {  
  24.         // TODO Auto-generated method stub  
  25.     }  
  26.   
  27.     /** 
  28.      * HttpSessionListener中的方法,回收session时,删除sessionMap中对应的session 
  29.      */  
  30.     public void sessionDestroyed(HttpSessionEvent event) {  
  31.         getSessionMap().remove(event.getSession().getId());  
  32.     }  
  33.   
  34.     /** 
  35.      * 得到在线用户会话集合 
  36.      */  
  37.     public static List<HttpSession> getUserSessions() {  
  38.         List<HttpSession> list = new ArrayList<HttpSession>();  
  39.         Iterator<String> iterator = getSessionMapKeySetIt();  
  40.         while (iterator.hasNext()) {  
  41.             String key = iterator.next();  
  42.             HttpSession session = getSessionMap().get(key);  
  43.             list.add(session);  
  44.         }  
  45.         return list;  
  46.     }  
  47.   
  48.     /** 
  49.      * 得到用户对应会话map,key为用户ID,value为会话ID 
  50.      */  
  51.     public static Map<String, String> getUserSessionMap() {  
  52.         Map<String, String> map = new HashMap<String, String>();  
  53.         Iterator<String> iter = getSessionMapKeySetIt();  
  54.         while (iter.hasNext()) {  
  55.             String sessionId = iter.next();  
  56.             HttpSession session = getSessionMap().get(sessionId);  
  57.             UserInfo user = (UserInfo) session.getAttribute("userInfo");  
  58.             if (user != null) {  
  59.                 map.put(user.getUserId(), sessionId);  
  60.             }  
  61.         }  
  62.         return map;  
  63.     }  
  64.   
  65.     /** 
  66.      * 移除用户Session 
  67.      */  
  68.     public synchronized static void removeUserSession(String userId) {  
  69.         Map<String, String> userSessionMap = getUserSessionMap();  
  70.         if (userSessionMap.containsKey(userId)) {  
  71.             String sessionId = userSessionMap.get(userId);  
  72.             getSessionMap().get(sessionId).invalidate();  
  73.             getSessionMap().remove(sessionId);  
  74.         }  
  75.     }  
  76.   
  77.     /** 
  78.      * 增加用户到session集合中 
  79.      */  
  80.     public static void addUserSession(HttpSession session) {  
  81.         getSessionMap().put(session.getId(), session);  
  82.     }  
  83.   
  84.     /** 
  85.      * 移除一个session 
  86.      */  
  87.     public static void removeSession(String sessionID) {  
  88.         getSessionMap().remove(sessionID);  
  89.     }  
  90.   
  91.     public static boolean containsKey(String key) {  
  92.         return getSessionMap().containsKey(key);  
  93.     }  
  94.   
  95.     /** 
  96.      * 判断该用户是否已重复登录,使用 
  97.      * 同步方法,只允许一个线程进入,才好验证是否重复登录 
  98.      * @param user 
  99.      * @return 
  100.      */  
  101.     public synchronized static boolean checkIfHasLogin(UserInfo user) {  
  102.         Iterator<String> iter = getSessionMapKeySetIt();  
  103.         while (iter.hasNext()) {  
  104.             String sessionId = iter.next();  
  105.             HttpSession session = getSessionMap().get(sessionId);  
  106.             UserInfo sessionuser = (UserInfo) session.getAttribute("userInfo");  
  107.             if (sessionuser != null) {  
  108.                 if (sessionuser.getUserId().equals(user.getUserId())){  
  109.                      版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
    原文链接:https://blog.csdn.net/bestone0213/article/details/47746693
    站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-28 02:40:27
  • 阅读 ( 775 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢