HttpSessionBindingListener在TomCat服务器上引发NullPointerException

时间:2018-10-10 14:35:58

标签: java spring-mvc tomcat listener httpsession

我正在使用 HttpSessionBindingListener 来维护Web应用程序中所有活动登录用户的记录。以下代码对于WildFly服务器效果很好,但在<<>上抛出 NullPointerException valueBound 方法中的strong> Tomcat ,同时获取在调用此方法之前设置的会话属性。

package com.abc.def.xyz.dto;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import com.abc.def.xyz.util.Logger;

public class ActiveLoginUserDetails implements Serializable,HttpSessionBindingListener {

    private Logger log = Logger.getLogger(this.getClass());

    private static final long serialVersionUID = 1L;
    private long usrId;
    private String usrCode;
    private String usrShortName;

    private static Map<ActiveLoginUserDetails, HttpSession> activeLoginDtls = new ConcurrentHashMap<ActiveLoginUserDetails, HttpSession>();
    private static Map<ActiveLoginUserDetails, HttpSession> loggedInUserDtlsMap = new ConcurrentHashMap<ActiveLoginUserDetails, HttpSession>();


    public long getUsrId() {
        return usrId;
    }
    public void setUsrId(long usrId) {
        this.usrId = usrId;
    }
    public String getUsrCode() {
        return usrCode;
    }
    public void setUsrCode(String usrCode) {
        this.usrCode = usrCode;
    }
    public String getUsrShortName() {
        return usrShortName;
    }
    public void setUsrShortName(String usrShortName) {
        this.usrShortName = usrShortName;
    }

    public static Map<ActiveLoginUserDetails, HttpSession> getActiveLoginDtls() {
        return activeLoginDtls;
    }
    public static void setActiveLoginDtls(Map<ActiveLoginUserDetails, HttpSession> activeLoginDtls) {
        ActiveLoginUserDetails.activeLoginDtls = activeLoginDtls;
    }
    public static Map<ActiveLoginUserDetails, HttpSession> getLoggedInUserDtlsMap() {
        return loggedInUserDtlsMap;
    }
    public static void setLoggedInUserDtlsMap(
            Map<ActiveLoginUserDetails, HttpSession> loggedInUserDtlsMap) {
        ActiveLoginUserDetails.loggedInUserDtlsMap = loggedInUserDtlsMap;
    }


    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        ActiveLoginUserDetails sessionUsrDtls = (ActiveLoginUserDetails) event.getSession().getAttribute("LoggedInUsers");
        Boolean userHasSmInteractiveLoginAction = false;
        Boolean multiLoginAllowed = false;
        Boolean check = true;
        log.info("sessionUsrDtls:::::"+ );

        List<ActionDetails> actnDtlsList = (List<ActionDetails>) event.getSession().getAttribute("sessionActnList");
        for(ActionDetails actnDtls : actnDtlsList){
            if(actnDtls.getActionAccessValue().equalsIgnoreCase("smLogin")){
                userHasSmInteractiveLoginAction = true;
            }
        }

        if(userHasSmInteractiveLoginAction){
            String amultiLoginAllowedUserCodeList = "adminUser";
            String[] usrCodeArr = amultiLoginAllowedUserCodeList.split(",");
            for(String tempUserCode : usrCodeArr){
                if(sessionUsrDtls.getUsrCode().equalsIgnoreCase(tempUserCode)){
                    multiLoginAllowed = true;
                }
            }
        }

        if(!multiLoginAllowed){
            for( Entry<ActiveLoginUserDetails, HttpSession> tempLoginDtls : activeLoginDtls.entrySet()){
                 ActiveLoginUserDetails temp = tempLoginDtls.getKey();
                 if(temp.getUsrCode().equals(sessionUsrDtls.getUsrCode())){
                     if (event.getSession() != null) {
                         check = false;
                         event.getSession().invalidate();
                         log.debug("After Invalidating duplicate session for "+sessionUsrDtls.getUsrCode());
                     }
                 }
             }
         }
        if(check){
             log.debug("Inside adding user from session:::");
             activeLoginDtls.put(this, event.getSession());
         }
    }


    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        log.debug("Inside removing user from session:::");
        activeLoginDtls.remove(this);
        loggedInUserDtlsMap.remove(this);
    }

}

它在行

上抛出NullPointerException(用于sessionUsrDtls对象)
if(sessionUsrDtls.getUsrCode().equalsIgnoreCase(tempUserCode)){

,表示该属性未设置为会话。但是此方法仅在将值设置为session后才会被调用,因此无法弄清这里到底发生了什么问题。

0 个答案:

没有答案