Java Web防止使用者重複登入(同一使用者同時登入)的一種實現方案__java

來源:互聯網
上載者:User
1.思路

在Java web項目中,有時需要防止使用者重複登入,解決方案有多種。比如Spring security就可以防止使用者重複登入。
這裡給出一個簡單的解決方案:在處理登入的login方法中,先查詢資料庫驗證下該使用者是否存在,如果存在 判斷該登入賬戶是否已經鎖定了, 然後從application內建範圍對象中取出所有的登入資訊,查看該username賬戶是否已經登入,如果登入了,就友好提示下,反之表示可以登入,將該登入資訊以索引值對的方式儲存在application中。
當使用者登出時,刪除application中相關資料即可。 2.範例程式碼 2.1 處理登入方法

    @RequestMapping("/checkLogin.do")    public String checkLogin(HttpSession session, String username, String password) {        System.out.println("checkLogin.do");        UserBean user=userService.login(username,password);        if(user!=null){//登入成功            //session.getServletContext()得到時application對象            ServletContext application=session.getServletContext();            Map<String, String> loginMap = (Map<String, String>)application.getAttribute("loginMap");            if(loginMap==null){                loginMap = new HashMap<>();            }            for(String key:loginMap.keySet()) {                if (user.getUsername().equals(key)) {                    if(session.getId().equals(loginMap.get(key))) {                        System.out.println(username+"在同一地點多次登入。");                    }else{                        System.out.println(username+"異地登入被拒絕。");                        session.setAttribute("tip", "該使用者已經異地登入。");                        return "forward:/index.jsp";                    }                }            }            loginMap.put(user.getUsername(),session.getId());            application.setAttribute("loginMap", loginMap);            session.setAttribute("username",user.getUsername());            System.out.println("登入成功。");            return "redirect:/index";        }else{            //登入失敗            System.out.println("登入失敗。");            session.setAttribute("tip","登入失敗。");            return "forward:/index.jsp";        }    }
2.2 銷毀Session
package cn.hadron.servlet;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import java.util.Map;/** * Created by root on 17-9-28. */public class SessionListener implements HttpSessionListener {    @Override    public void sessionCreated(HttpSessionEvent event) {    }    @Override    public void sessionDestroyed(HttpSessionEvent event) {        //在session銷毀的時候 把loginMap中儲存的索引值對清除        String username = event.getSession().getAttribute("username").toString();        if(username!=null){            Map<String, String> loginMap = (Map<String, String>)event.getSession().getServletContext().getAttribute("loginMap");            loginMap.remove(username);            event.getSession().getServletContext().setAttribute("loginMap",loginMap);            System.out.println(username+"使用者登出。");        }    }}
2.3 web.xml
  <listener>    <listener-class>cn.hadron.servlet.SessionListener</listener-class>  </listener>
2.4 登入頁面
${tip}<form action="/user/checkLogin.do" method="post">    帳號:<input type="text" name="username"><br>    口令:<input type="password" name="password"><br>    <input type="submit" value="登入"></form>
3 測試 3.1 本地登入



注意:本地可以多次登入。 3.2 異地登入

另找一台機器測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.