springboot控制统一用户在不同设备上登录以及获取登陆人信息及登陆系统人数

springboot控制统一用户在不同设备上登录:
直接看如下代码:

package com.kd.system.utils;

import org.apache.tomcat.jni.Thread;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@WebListener
public class SpringSessionListener implements HttpSessionListener,Runnable {

    /**
     * 保存session和username的映射
      */
    private static HashMap hUserName = new HashMap();
    private static HashMap hSessionMap = new HashMap();

    //private Thread thread;
    private static int MaxSession;
    private static int activeSession;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println(isOnline(se.getSession())+"登录状态");
        addSession();
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("退出");
        delSession();
        hUserName.remove(se.getSession().getId());
        hSessionMap.remove(se.getSession().getId());
    }

    /**
     * 判断用户是否已经登录及处理方法
     * @author LW
     * @date 19/8/23
     * @param session
     * @param sUserName 登录名
     * @return 用户是否已经登录的标志
     */
    public static boolean isAlreadyEnter(HttpSession session, String sUserName){
        boolean flag = false;
        // 如果该用户已经登录过,则使用上次登录的用户掉线
        if(hUserName.containsValue(sUserName)){
            flag = true;
            // 遍历原来的hUserName,删除原来用户名对应的sessionId,即删除原来的sessionId和username
            Iterator iter = hUserName.entrySet().iterator();
            while(iter.hasNext()){
                Map.Entry entry = (Map.Entry)iter.next();
                Object key  = entry.getKey();
                Object val = entry.getValue();
                if(((String)val).equals(sUserName)){
                    HttpSession session1= (HttpSession) hSessionMap.get(key);
                    session1.removeAttribute("paramsList");
                    hUserName.remove(key);
                }
            }
            // 添加当前用户的sessionId和username
            hUserName.put(session.getId(), sUserName);
            hSessionMap.put(session.getId(), session);
        }
        // 如果没有登录直接添加当前用户的sessionId和username
        else{
            flag = false;
            hUserName.put(session.getId(), sUserName);
            hSessionMap.put(session.getId(), session);
        }
        return flag;
    }

    /**
     * 判断用户是否在线
     * @author LW
     * @date 19/8/23
     * @param session
     * @return
     */
    public static boolean isOnline(HttpSession session){
        boolean flag = true;
        if(hUserName.containsValue(session.getId())){
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }

    /**
     * OnlineSessionListener监听器(实现HttpSessionListener)接口来实现页面在线访问人数统计,
     * @author LW
     * @date 19/8/23
     */
    public static void addSession(){
        activeSession++;
        if(activeSession>=MaxSession){
            MaxSession = activeSession;
        }
    }

    public static void delSession(){
        if(activeSession>0){
            activeSession--;
        }
    }

    /**
     * 获取在线人数
     * @author LW
     * @date 19/8/23
     * @return
     */
    public static int getActiveNum(){
        return activeSession;
    }

    /**
     * 获取累计访问人数
     * @author LW
     * @date 19/8/23
     * @return
     */
    public static int getMaxSessionNum(){
        return MaxSession;
    }


}

你可能感兴趣的:(部分知识,java,spring,spring,boot)