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

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.  

SSH版最大会话连接数的更多相关文章

  1. 《linux就该这么学》课堂笔记13 网络会话、ssh、远程会话

    1.常见的网卡绑定驱动有三种模式—mode0.mode1和mode6 mode0(平衡负载模式):平时两块网卡均工作,且自动备援,但需要在与服务器本地网卡相连的交换机设备上进行端口聚合来支持绑定技术. ...

  2. ORA-00020: maximum number of processes (40) exceeded模拟会话连接数满

    问题描述:在正式生产环境中,有的库建的process和session连接数目设置的较小,导致后期满了无法连接.因为正式库无法进行停库修改,只能释放连接,做个测试模拟 1. 修改现有最大会话与进程连接数 ...

  3. 解决SSH会话连接超时问题

    用SSH客户端连接linux服务器时,经常会出现与服务器会话连接中断现象,照成这个问题的原因便是SSH服务有自己独特的会话连接机制.记得在一年前就有朋友问过我这个问题,那时候我便是草草打发,结果自己现 ...

  4. 阿里云(ECS)Linux客户端SSH会话连接超时OperationTimedOut

    问题描述:使用SecureCRT等SSH客户端连接Linux服务器时,提示Operation timed out. 问题原因:SSH服务未配置或注释掉向SSH客户端连接会话发送频率和时间. 解决方法: ...

  5. SSH的端口转发:本地转发Local Forward和远程转发Remote Forward

    关于使用ssh portforwarding来进行FQ的操作,网络上已经有很多很好的文章,我在这里只是画两个图解释一下. 首先要记住一件事情就是: SSH 端口转发自然需要 SSH 连接,而 SSH ...

  6. Android手机SSH Client客户端推荐JuiceSSH

    Windows上建立ssh服务器 参见: http://www.cnblogs.com/xred/archive/2012/04/21/2461627.html Android手机SSH Client ...

  7. SSH服务端配置、优化加速、安全防护

    CentOS7自带的SSH服务是OpenSSH中的一个独立守护进程SSHD.由于使用telnet在网络中是明文传输所以用其管理服务器是非常不安全的不安全,SSH协议族可以用来对服务器的管理以及在计算机 ...

  8. Redhat 6.7 x64升级SSH到OpenSSH_7.4p1完整文档

    原文链接:https://www.cnblogs.com/xshrim/p/6472679.html 导语 Redhat企业级系统的6.7版自带SSH版本为OpenSSH_5.3p1, 基于审计和安全 ...

  9. SSH的端口转发

    这里是一篇很好的介绍SSH PortForwarding的文章http://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/ 可以将远端服务器一 ...

随机推荐

  1. [SoapUI] Tips and Tricks(提示和技巧)

    https://www.soapui.org/scripting-properties/tips-tricks.html

  2. 《Delphi XE6 android 编程入门教程》推荐

    近5.6年已经没有看见关于delphi的新技术的书出来了(看来在国内delphi的使用量确实很低了), 高勇同学最近出了一本<Delphi XE6 android 编程入门教程>,上周刚拿 ...

  3. 2018.08.17 洛谷[POI2010]GRA-The Minima Game(线性dp)

    传送门 短代码神奇dp. 自己yy的思路居然1A了好高兴啊! 不难想到每个人选择的时候一定是取连续的最大的那一段数,自然需要先排序. 然后可以用dp[i]表示当前最大数是a[i]的时候先手可以获得的最 ...

  4. RESTful Web API 实践

    REST 概念来源 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备...). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通 ...

  5. 优秀前端工程师必备:" checkbox & radio--单钩 & 多钩 "大比较:你是♂||♀ , 还是 ♂&♀

    1 单选: type="radio"  需求: 男女input只能选择一个 <input type="radio" name="sex" ...

  6. 都有哪些 cache ?

    1. spring http://www.springframework.org/schema/cache 2. ehcache LOGO关键词:palindrome [ˈpælɪndrəʊm] 正读 ...

  7. 一种基于Redis的10行代码实现IP频率控制方法

    优点:可支持海量访问的频率控制,只需要增加Redis机器,单个Redis节点(只占用一个cpu core)即可支持10万/s以上的处理. 基于IP频率限制是种常见需求,基于Redis可以十分简单实现对 ...

  8. The First Android App----Adding the Action Bar

    In its most basic form, the action bar displays the title for the activity and the app icon on the l ...

  9. Python学习-2.安装IDE

    Python安装包中已经包含了一个IDE了,叫IDLE,可以在Python的安装目录内找到路径为 ./Lib/idlelib/idle.bat 或者可以在开始菜单中找到. 但是这个IDE功能很弱,缺少 ...

  10. Git安全配置

      今天收到了阿里云异地登录的短信报警,登录阿里云后台发现,有人从深圳登录了我的服务器(本人在北京),查看详细信息一共登录了5次,前两次是使用的git用户进行登录,后两次已经变成了root用户,怀疑是 ...