1. 程式人生 > >HttpSessionListener 實現類 監聽 session創建於銷燬時的動作

HttpSessionListener 實現類 監聽 session創建於銷燬時的動作

監聽 session創建於銷燬時的動作  ,用於使用者安全相關問題是的統計,登出等操作

使用方法: 在web.xml檔案中新增listener 
<listener>
    <listener-class>路徑.sessionlistener</listener-class>
  </listener> 



package com.fnic.cdnmgr.ui.security.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fnic.cdnmgr.sdk.comm.CDNException;
import com.fnic.cdnmgr.sdk.comm.ConstComm;
import com.fnic.cdnmgr.sdk.security.api.IUserInOutSrv;
import com.fnic.cdnmgr.ui.security.cache.OnlineSessionCache;

/**
 * Application Lifecycle Listener implementation class sessionlistener
 * 
 */
@WebListener
public class sessionlistener implements HttpSessionListener, HttpSessionAttributeListener
{

    private static final Logger logger = LoggerFactory.getLogger(ConstComm.LOGGER_SC);

    private static IUserInOutSrv userInOutSrv;

    private static OnlineSessionCache onlineSessionCache;

    /**
     * Default constructor.
     */
    public sessionlistener()
    {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    @Override
    public void attributeRemoved(final HttpSessionBindingEvent arg0)
    {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    @Override
    public void attributeAdded(final HttpSessionBindingEvent arg0)
    {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    @Override
    public void attributeReplaced(final HttpSessionBindingEvent arg0)
    {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    @Override
    public void sessionCreated(final HttpSessionEvent arg0)
    {
        logger.debug("sessioncreated");

    }

    /**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    @Override
    public void sessionDestroyed(final HttpSessionEvent event)
    {
        HttpSession session = event.getSession();
        String tokenId = "";
        if (session.getAttribute("loginToken") != null)
        {
            tokenId = session.getAttribute("loginToken").toString();
        }
        try
        {
            userInOutSrv.logout(tokenId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        session.invalidate();
        onlineSessionCache.deleteSession(session);
    }

    public static void setUserInOutSrv(final IUserInOutSrv userInOutSrv)
    {
        sessionlistener.userInOutSrv = userInOutSrv;
    }

    public static void setOnlineSessionCache(final OnlineSessionCache onlineSessionCache)
    {
        sessionlistener.onlineSessionCache = onlineSessionCache;
    }

}