SSH版最大会话连接数

时间:2023-03-09 05:22:33
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. public class SessionListener implements HttpSessionListener,ServletRequestListener,HttpSessionAttributeListener{
    16. public static Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
    17. public static Map<String, HttpSession> sessionMap1 = new HashMap<String, HttpSession>();
    18. private static Boolean onlyOne = Boolean.valueOf(Message.getMessage("session.onlyone"));
    19. private HttpServletRequest request ;
    20. /**获取request对象*/
    21. public void requestInitialized(ServletRequestEvent event) {
    22. request = (HttpServletRequest)event.getServletRequest();
    23. }
    24. /**以下是实现HttpSessionListener中的方法:该方法登录与否都会执行**/
    25. public void sessionCreated(HttpSessionEvent se){
    26. }
    27. /**以下是实现HttpSessionListener中的方法**/
    28. public void sessionDestroyed(HttpSessionEvent se){
    29. hUserName.remove(se.getSession().getId());
    30. UserObject.remove(se.getSession().getId());
    31. if(sessionMap!=null){
    32. sessionMap.remove(se.getSession().getId());
    33. }
    34. if(sessionMap1!=null){
    35. sessionMap1.remove(se.getSession().getId());
    36. }
    37. }
    38. /**
    39. * isAlreadyEnter-用于判断用户是否已经登录以及相应的处理方法
    40. * <该方法是系统业务的方法,不是处理单个用户登录的问题,以该方法做为程序的入口>
    41. */
    42. public static boolean isAlreadyEnter(HttpSession session,String sUserName,LoginUserInfo loginTriggers){
    43. boolean flag = false;
    44. return flag;
    45. }
    46. /**
    47. * 此方法,可以在登录时候添加一个session 用以判断是否已经登录,然后再进行登录人登录控制
    48. */
    49. public void attributeAdded(HttpSessionBindingEvent event) {
    50. //如果只允许一个账号一处登陆,单台客户端电脑只允许一个用户登录
    51. if(onlyOne.booleanValue()){
    52. String name = event.getName();
    53. if(name.equals("isLoginIn")){
    54. // 单台客户端电脑只允许一个用户登录
    55. String ipAddr = this.getIpAddr(request);
    56. //如果原先已登录,则踢出原先登陆的
    57. if(sessionMap1.containsKey(ipAddr) ){
    58. try{
    59. sessionMap1.get(ipAddr).invalidate();
    60. }catch(Exception e){}
    61. sessionMap1.remove(ipAddr);
    62. }
    63. if(ipAddr != null && event.getSession().isNew())
    64. sessionMap1.put(ipAddr, event.getSession());
    65. //只允许一个账号一个客户端登陆
    66. String userName= getUserName(event);
    67. if(sessionMap.containsKey(userName) ){
    68. try{
    69. sessionMap.get(userName).invalidate();
    70. }catch(Exception e){}
    71. sessionMap.remove(userName);
    72. }
    73. if(userName != null && event.getSession().isNew())
    74. sessionMap.put(userName, event.getSession());
    75. }
    76. }
    77. }
    78. public static String getIpAddr(HttpServletRequest request) {
    79. String ip = request.getHeader("x-forwarded-for");
    80. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    81. ip = request.getHeader("Proxy-Client-IP");
    82. }
    83. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    84. ip = request.getHeader("WL-Proxy-Client-IP");
    85. }
    86. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    87. ip = request.getRemoteAddr();
    88. }
    89. return ip;
    90. }
    91. /*获取session中存储的用户名*/
    92. private String getUserName(HttpSessionBindingEvent se) {
    93. String userName = null;
    94. return userName;
    95. }
    96. public void attributeRemoved(HttpSessionBindingEvent event) {
    97. // TODO Auto-generated method stub
    98. }
    99. public void attributeReplaced(HttpSessionBindingEvent arg0) {
    100. // TODO Auto-generated method stub
    101. }
    102. public void requestDestroyed(ServletRequestEvent arg0) {
    103. // TODO Auto-generated method stub
    104. }