Java web工程判斷使用者是否重複登入

來源:互聯網
上載者:User

前幾天,網上找了些朋友的資料,做了一個小功能,驗證使用者是否重複登入。

原理就是:每一個使用者,登入前有一個驗證,當第一次登入時,會把其session資訊,添加到一個特定的靜態變數中。當第二次登入時,驗證到靜態變數中存在該使用者的資訊,就表示為重複登入。

jsp代碼,一個form表單提交:

<form action="/struts2upload/system/login/reLogin.action"method="post">使用者名稱:<input type="text" id="txtUser" name="txtUser" value="" />密  碼:<input type="text" id="txtPass" name="txtPass" value="" /><input type="submit" id="subOk" value="確定" /></form>

struts2配置:

<action name="reLogin" class="userLoginAction" method="reLogin"><result name="input" type="redirect">/relogin.jsp</result><result name="success" type="redirect">/ok.jsp</result></action>

action代碼:

/* * 可判斷使用者是否重複登入 */public String reLogin() {String userId = this.getTxtUser();//ServletActionContext.getRequest().getParameter("txtUser");UserInfo user = new UserInfo();user.setUserId(userId);//驗證該使用者ID,是否已經登入。目前使用者比較已登入到系統的靜態變數中的值,是否存在。Boolean hasLogin = SessionUserListener.checkIfHasLogin(user);// 如果重複登入,控制端則列印資訊,返回登入頁面if (hasLogin) {System.out.println(user.getUserId()+"已經登入到本系統。");return "input";// SessionUserListener.removeUserSession(userId);} else {// 如果沒有重複登入,則將該登入的使用者資訊添加入session中ServletActionContext.getRequest().getSession().setAttribute("userInfo", user);// 比較儲存所有使用者session的靜態變數中,是否含有當前session的索引值映射,如果含有就刪除if (SessionUserListener.containsKey(ServletActionContext.getRequest().getSession().getId())) {SessionUserListener.removeSession(ServletActionContext.getRequest().getSession().getId());}//把目前使用者封裝的session按,sessionID和session進行索引值封裝,添加到靜態變數map中。SessionUserListener.addUserSession(ServletActionContext.getRequest().getSession());}return "success";}

session監聽類:

package com.zyujie.listener;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import com.zyujie.pojo.UserInfo;public class SessionUserListener implements HttpSessionListener {// key為sessionId,value為HttpSession,使用static,定義靜態變數,使之程式運行時,一直存在記憶體中。private static java.util.Map<String, HttpSession> sessionMap = new java.util.concurrent.ConcurrentHashMap<String, HttpSession>(500);/** * HttpSessionListener中的方法,在建立session */public void sessionCreated(HttpSessionEvent event) {// TODO Auto-generated method stub}/** * HttpSessionListener中的方法,回收session時,刪除sessionMap中對應的session */public void sessionDestroyed(HttpSessionEvent event) {getSessionMap().remove(event.getSession().getId());}/** * 得到線上使用者會話集合 */public static List<HttpSession> getUserSessions() {List<HttpSession> list = new ArrayList<HttpSession>();Iterator<String> iterator = getSessionMapKeySetIt();while (iterator.hasNext()) {String key = iterator.next();HttpSession session = getSessionMap().get(key);list.add(session);}return list;}/** * 得到使用者對應會話map,key為使用者ID,value為會話ID */public static Map<String, String> getUserSessionMap() {Map<String, String> map = new HashMap<String, String>();Iterator<String> iter = getSessionMapKeySetIt();while (iter.hasNext()) {String sessionId = iter.next();HttpSession session = getSessionMap().get(sessionId);UserInfo user = (UserInfo) session.getAttribute("userInfo");if (user != null) {map.put(user.getUserId(), sessionId);}}return map;}/** * 移除使用者Session */public synchronized static void removeUserSession(String userId) {Map<String, String> userSessionMap = getUserSessionMap();if (userSessionMap.containsKey(userId)) {String sessionId = userSessionMap.get(userId);getSessionMap().get(sessionId).invalidate();getSessionMap().remove(sessionId);}}/** * 增加使用者到session集合中 */public static void addUserSession(HttpSession session) {getSessionMap().put(session.getId(), session);}/** * 移除一個session */public static void removeSession(String sessionID) {getSessionMap().remove(sessionID);}public static boolean containsKey(String key) {return getSessionMap().containsKey(key);}/** * 判斷該使用者是否已重複登入,使用 * 同步方法,只允許一個線程進入,才好驗證是否重複登入 * @param user * @return */public synchronized static boolean checkIfHasLogin(UserInfo user) {Iterator<String> iter = getSessionMapKeySetIt();while (iter.hasNext()) {String sessionId = iter.next();HttpSession session = getSessionMap().get(sessionId);UserInfo sessionuser = (UserInfo) session.getAttribute("userInfo");if (sessionuser != null) {if (sessionuser.getUserId().equals(user.getUserId())){return true;}}}return false;}/** * 擷取線上的sessionMap */public static Map<String, HttpSession> getSessionMap() {return sessionMap;}/** * 擷取線上sessionMap中的SessionId */public static Iterator<String> getSessionMapKeySetIt() {return getSessionMap().keySet().iterator();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.