单态登录

单态登录

1、什么是单态登录?

单态登录就是一个账号只能在一台机器上登录,如果在其他机器上登录了,则原来的登录自动失效。单态登录的目的是防止多台机器同时使用一个账号。

2、单态登录的实现

单态登录使用监听器,通过监控 Session 的 personInfo 属性是否添加删除实现的。如果新添加了一个personInfo 的属性,则认为是新登录用户,Listener 中 查看该用户的账号是否已经在其他机器上登录。如果已经登录过了,则旧的登录信息失效。

Login.java

package com.openlab.listener;

import java.util.HashMap;  
import java.util.Map;  
import javax.servlet.http.HttpSession;  
import javax.servlet.http.HttpSessionAttributeListener;  
import javax.servlet.http.HttpSessionBindingEvent;  
import org.apache.commons.logging.Log;  
import org.apache.commons.logging.LogFactory;  
   
public class Login implements HttpSessionAttributeListener {  
   Log log= LogFactory.getLog(this.getClass());  
   Map<String,HttpSession> map = new HashMap<String,HttpSession>();  
    public Login() {}  

    public void attributeRemoved(HttpSessionBindingEvent event)  {   
        String name  = event.getName();  
        if(name.equals("personInfo")){  
            PersonInfo personInfo = (PersonInfo) event.getValue();  
            map.remove(personInfo.getAccount());  
            log.info("账号"+personInfo.getAccount()+"注销");  
        }  
    }  
  
    public void attributeAdded(HttpSessionBindingEvent event)  {   
           
        String name = event.getName();  
        if(name.equals("personInfo")){  
            PersonInfo personInfo = (PersonInfo) event.getValue();  
            if(map.get(personInfo.getAccount()) != null){  
                HttpSession session = map.get(personInfo.getAccount());  
                  
                PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");  
                log.info("账号"+oldPersonInfo.getAccount()+"在"+oldPersonInfo.getIp()+"已经登录,该登录将被迫下线!");  
                session.removeAttribute("personInfo");  
                session.setAttribute("msg", "您的账号已经在其他机器上登录,您被迫下线!");  
                  
            }  
            map.put(personInfo.getAccount(), event.getSession());  
            log.info("账号"+personInfo.getAccount()+"在"+personInfo.getIp()+"登录");  
              
        }  
    }  
  
    public void attributeReplaced(HttpSessionBindingEvent event)  {   
         
        String name = event.getName();  
        if(name.equals("personInfo")){  
            PersonInfo oldPersonInfo = (PersonInfo) event.getValue();  
            //移除旧的登录信息  
            map.remove(oldPersonInfo.getAccount());  
            //新的登录信息  
            PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo");  
            //也要检查新的账号是否在别的机器上登录  
            if(map.get(personInfo.getAccount()) != null){  
                HttpSession session = map.get(personInfo.getAccount());  
                  
                session.removeAttribute("personInfo");  
                session.setAttribute("msg", "您的账号已经在其他机器上登录,您被迫下线!");  
                  
            }  
            map.put(personInfo.getAccount(), event.getSession());  
            log.info("账号"+personInfo.getAccount()+"在"+personInfo.getIp()+"登录");  
              
        }  
    }  
      
}  

如果Session中有PersonInfo 信息,则表示已经登录,页面将显示登录后的账号。如果Session中没有PersonInfo信息,则表示没有登录,页面将显示登录输入框。登录与注销动作都在该JSP 中完成。

listener.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
   <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
   <jsp:directive.page import="com.openlab.listener.PersonInfo" />  
   <%  
    String action = request.getParameter("action");  
    String account = request.getParameter("account");  
    if("login".equals(action) && account.trim().length()>0){  
        PersonInfo personInfo = new PersonInfo();  
        personInfo.setAccount(account);  
        personInfo.setIp(request.getRemoteAddr());  
        personInfo.setLoginDate(new java.util.Date());  
        session.setAttribute("personInfo",personInfo);  
        response.sendRedirect(response.encodeRedirectURL(request.getRequestURI()));  
        return;  
    }  
    else if("logout".equals(action)){  
        session.removeAttribute("personInfo");  
        response.sendRedirect(response.encodeRedirectURL(request.getRequestURI()));  
        return;  
    }  
   %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  
<title>Insert title here</title>  
</head>  
<body>  
  
    <c:choose>  
        <c:when test="${ personInfo != null}">  
            欢迎您,${personInfo.account}<br/>  
            您的登录ip为,${personInfo.ip}<br>  
            登录时将为,<fmt:formatDate value="${personInfo.loginDate}" pattern="yyyy-MM-dd HH:mm"/><br/>  
            <a href="${pageContext.request.requestURI}?action=logout">退出</a>  
    <!-- 每五秒钟刷新一次页面 -->  
        <script>setTimeout("location=location;", 5000);</script>  
        </c:when>  
        <c:otherwise>  
         
            <c:remove var="msg" scope="session"/>  
            <form action="${pageContext.request.requestURI}?action=login" method="post">  
                账号:<input name="account">  
                <input type="submit" value="登录">  
            </form>  
        </c:otherwise>  
    </c:choose>  
</body>  
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>loginListener</display-name>
    <listener>
    <listener-class>com.openlab.listener.Login</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>listener.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值