首页 技术 正文
技术 2022年11月10日
0 收藏 849 点赞 5,162 浏览 3552 个字

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

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.  
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905