1. 程式人生 > >java web開發一個帳號同一時間只能一個人登入(單點登入)

java web開發一個帳號同一時間只能一個人登入(單點登入)

對於一個帳號在同一時間只能一個人登入,可以通過下面的方法實現:
1 .在使用者登入時,把使用者新增到一個ArrayList中
2 .再次登入時檢視ArrayList中有沒有該使用者,如果ArrayList中已經存在該使用者,則阻止其登入
3 .當用戶退出時,需要從該ArrayList中刪除該使用者,這又分為三種情況
① 使用登出按鈕正常退出
② 點選瀏覽器關閉按鈕或者用Alt+F4退出,可以用JavaScript捕捉該頁面關閉事件,
執行一段Java方法刪除ArrayList中的使用者
③ 非正常退出,比如客戶端系統崩潰或突然宕機,可以採用隔一段時間session沒活動就刪除該session所對應的使用者來解決,這樣使用者需要等待一段時間之後就可以正常登入。

在LoginAction中定義:
// 用來在伺服器端儲存登入的所有帳號
public static List logonAccounts;

login() 登入方法中:
// 設定session不活動時間為30分
request.getSession().setMaxInactiveInterval(60*30);
if(logonAccounts==null){
    logonAccounts = new ArrayList();
}
// 檢視ArrayList中有沒有該使用者
for (int i = 0; i < logonAccounts.size(); i++) {
    Account existAccount = (Account)logonAccounts.get(i);
    if(account.getAccountId().equals(existAccount.getAccountId())){
        return "denied";
    }
}
// 在使用者登入時,把sessionId新增到一個account物件中
// 在後面 ③ 需要根據此sessionId刪除相應使用者
account.setSessionId(request.getSession().getId());
// 該使用者儲存到ArrayList靜態類變數中
logonAccounts.add(account);
return "login";

① 使用登出按鈕正常退出
logout() 退出方法中:
if(logonAccounts==null){
    logonAccounts = new ArrayList();
}
// 刪除ArrayList中的使用者  ⑴
for (int i = 0; i < logonAccounts.size(); i++) {
    Account existAccount = (Account)logonAccounts.get(i);
    if(account.getAccountId().equals(existAccount.getAccountId())){
        logonAccounts.remove(account);
    }
}

② 點選瀏覽器關閉按鈕或者用Alt+F4退出:
在後臺彈出一個視窗,在彈出視窗中刪除ArrayList中的使用者
function window.onbeforeunload(){
// 是否通過關閉按鈕或者用Alt+F4退出
// 如果為重新整理觸發onbeforeunload事件,下面if語句不執行
    if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){
        window.open('accountUnbound.jsp','',
                'height=0,width=0,top=10000,left=10000');
    }
}
accountUnbound.jsp : 彈出視窗中刪除ArrayList中的使用者
<%
    Account account = (Account) request.getSession().getAttribute("account");
    if(account != null){
        if(LoginAction.logonAccounts==null){
            LoginAction.logonAccounts = new ArrayList();
        }
        // 刪除ArrayList中的使用者——下面程式碼和上面的 ⑴ 處一樣
        for (int i = 0; i < logonAccounts.size(); i++) {
            Account existAccount = (Account)logonAccounts.get(i);
            if(account.getAccountId().equals(existAccount.getAccountId())){
                logonAccounts.remove(account);
           }
        }
    }
%>

為了保證上面程式碼可以執行完畢,3秒後關閉此彈出視窗(也位於accountUnbound.jsp中)
<script>
setTimeout("closeWindow();",3000);
function closeWindow(){
    window.close();
}
</script>

③ 使LoginAction 實現implements HttpSessionListener,並實現sessionCreated,sessionDestroyed方法,在sessionDestroyed中刪除ArrayList中的使用者(使用者超過30分鐘不活動則執行此方法)
public void sessionDestroyed(HttpSessionEvent event) {
   // 取得不活動時的sessionId,並根據其刪除相應logonAccounts中的使用者
   String sessionId = event.getSession().getId();
   for (int i = 0; i < logonAccounts.size(); i++) {
       Account existAccount = (Account)logonAccounts.get(i);
       if(account.getSessionId().equals(existAccount.getSessionId())){
           logonAccounts.remove(account);
       }
   }
}

注:

對於上面的,由於彈出視窗很容易被防火牆或者安全軟體阻攔,造成無法彈出視窗,從而短時間不能登入,這種情況可以用AJAX來代替彈出視窗,同樣在後臺執行刪除使用者的那段程式碼,卻不會受到防火牆限制:
<script>
    // <![CDATA[
    var http_request = false;
    function makeRequest(url) {
        http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
           if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
          try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                }
            }
        }
        if (!http_request) {
           alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, true);
        http_request.send(null);