zoukankan      html  css  js  c++  java
  • SSH版最大会话连接数

    在登录方法中加入如下两行语句,作为程序的入口:

     SessionListener.isAlreadyEnter(getHttpRequest().getSession(),this.getUserCode(),loginUser)

    getHttpRequest().getSession().setAttribute("isLoginIn", "LoginIn");

    在SessionListener类中做相关的踢出处理:

      1. import java.util.HashMap;
      2.  
        import java.util.Iterator;
      3.  
        import java.util.Map;
      4.  
        import javax.servlet.ServletRequestEvent;
      5.  
        import javax.servlet.ServletRequestListener;
      6.  
        import javax.servlet.http.HttpServletRequest;
      7.  
        import javax.servlet.http.HttpSession;
      8.  
        import javax.servlet.http.HttpSessionAttributeListener;
      9.  
        import javax.servlet.http.HttpSessionBindingEvent;
      10.  
        import javax.servlet.http.HttpSessionEvent;
      11.  
        import javax.servlet.http.HttpSessionListener;
      12.  
        import org.apache.struts2.ServletActionContext;
      13.  
        import com.hhwy.iepip.framework.message.Message;
      14.  
        import com.opensymphony.xwork2.ActionContext;
      15.  
         
      16.  
        public class SessionListener implements HttpSessionListener,ServletRequestListener,HttpSessionAttributeListener{
      17.  
        public static Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
      18.  
        public static Map<String, HttpSession> sessionMap1 = new HashMap<String, HttpSession>();
      19.  
        private static Boolean onlyOne = Boolean.valueOf(Message.getMessage("session.onlyone"));
      20.  
        private HttpServletRequest request ;
      21.  
         
      22.  
        /**获取request对象*/
      23.  
        public void requestInitialized(ServletRequestEvent event) {
      24.  
        request = (HttpServletRequest)event.getServletRequest();
      25.  
        }
      26.  
         
      27.  
        /**以下是实现HttpSessionListener中的方法:该方法登录与否都会执行**/
      28.  
        public void sessionCreated(HttpSessionEvent se){
      29.  
        }
      30.  
         
      31.  
        /**以下是实现HttpSessionListener中的方法**/
      32.  
        public void sessionDestroyed(HttpSessionEvent se){
      33.  
        hUserName.remove(se.getSession().getId());
      34.  
        UserObject.remove(se.getSession().getId());
      35.  
        if(sessionMap!=null){
      36.  
        sessionMap.remove(se.getSession().getId());
      37.  
        }
      38.  
        if(sessionMap1!=null){
      39.  
        sessionMap1.remove(se.getSession().getId());
      40.  
        }
      41.  
        }
      42.  
        /**
      43.  
        * isAlreadyEnter-用于判断用户是否已经登录以及相应的处理方法
      44.  
        * <该方法是系统业务的方法,不是处理单个用户登录的问题,以该方法做为程序的入口>
      45.  
        */
      46.  
        public static boolean isAlreadyEnter(HttpSession session,String sUserName,LoginUserInfo loginTriggers){
      47.  
        boolean flag = false;
      48.  
        return flag;
      49.  
        }
      50.  
         
      51.  
        /**
      52.  
        * 此方法,可以在登录时候添加一个session 用以判断是否已经登录,然后再进行登录人登录控制
      53.  
        */
      54.  
        public void attributeAdded(HttpSessionBindingEvent event) {
      55.  
        //如果只允许一个账号一处登陆,单台客户端电脑只允许一个用户登录
      56.  
        if(onlyOne.booleanValue()){
      57.  
        String name = event.getName();
      58.  
        if(name.equals("isLoginIn")){
      59.  
        // 单台客户端电脑只允许一个用户登录
      60.  
        String ipAddr = this.getIpAddr(request);
      61.  
        //如果原先已登录,则踢出原先登陆的
      62.  
        if(sessionMap1.containsKey(ipAddr) ){
      63.  
        try{
      64.  
        sessionMap1.get(ipAddr).invalidate();
      65.  
        }catch(Exception e){}
      66.  
        sessionMap1.remove(ipAddr);
      67.  
        }
      68.  
        if(ipAddr != null && event.getSession().isNew())
      69.  
        sessionMap1.put(ipAddr, event.getSession());
      70.  
         
      71.  
        //只允许一个账号一个客户端登陆
      72.  
        String userName= getUserName(event);
      73.  
        if(sessionMap.containsKey(userName) ){
      74.  
        try{
      75.  
        sessionMap.get(userName).invalidate();
      76.  
        }catch(Exception e){}
      77.  
        sessionMap.remove(userName);
      78.  
        }
      79.  
        if(userName != null && event.getSession().isNew())
      80.  
        sessionMap.put(userName, event.getSession());
      81.  
        }
      82.  
        }
      83.  
        }
      84.  
         
      85.  
        public static String getIpAddr(HttpServletRequest request) {
      86.  
        String ip = request.getHeader("x-forwarded-for");
      87.  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      88.  
        ip = request.getHeader("Proxy-Client-IP");
      89.  
        }
      90.  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      91.  
        ip = request.getHeader("WL-Proxy-Client-IP");
      92.  
        }
      93.  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      94.  
        ip = request.getRemoteAddr();
      95.  
        }
      96.  
        return ip;
      97.  
        }
      98.  
         
      99.  
        /*获取session中存储的用户名*/
      100.  
        private String getUserName(HttpSessionBindingEvent se) {
      101.  
        String userName = null;
      102.  
        return userName;
      103.  
        }
      104.  
         
      105.  
        public void attributeRemoved(HttpSessionBindingEvent event) {
      106.  
        // TODO Auto-generated method stub
      107.  
        }
      108.  
         
      109.  
        public void attributeReplaced(HttpSessionBindingEvent arg0) {
      110.  
        // TODO Auto-generated method stub
      111.  
        }
      112.  
         
      113.  
        public void requestDestroyed(ServletRequestEvent arg0) {
      114.  
        // TODO Auto-generated method stub
      115.  
        }
      116.  
  • 相关阅读:
    Chapter 7、面向对象(二)---类 (4th,Mar.)
    Chapter 7、面向对象(一)--- 概述
    值类型,引用类型
    Chapter 6、字符串(二)(1st,Mar.)
    练习:判断字符串“mingrikejijavabu”中,字符“i”出现了几次,并将结果输出。
    函数对象
    顺序容器:双向链表 list
    顺序容器:vector和deque 的程序举栗子
    STL中的 “大”、“小”和 “相等”
    标准模板库STL中常用的算法
  • 原文地址:https://www.cnblogs.com/yj716716yj/p/9474184.html
Copyright © 2011-2022 走看看