⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessionfilter.java

📁 intra_mart是日本NDD公司开发的著名企业应用解决方案中间件。集成了J2ee服务器
💻 JAVA
字号:
/*
 * SessionFilter.java
 *
 * Created on 2001/12/17, 18:59
 */

package jp.co.intra_mart.framework.base.session;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;

import java.io.IOException;

import javax.servlet.ServletException;

/**
 * 僙僢僔儑儞娗棟傪偟傑偡丅
 *
 * @author INTRAMART
 * @version 1.0
 */
public class SessionFilter implements Filter {

    /**
     * 僙僢僔儑儞儅僱乕僕儍
     */
    private SessionManager sessionManager;

    /**
     * 僙僢僔儑儞僾儘僷僥傿僴儞僪儔
     */
    private SessionPropertyHandler sessionHandler;

    /**
     * Filter Config
     */    
    private FilterConfig filterConfig;

    /**
     * 僙僢僔儑儞梡偺僼傿儖僞傪弶婜壔偟傑偡丅
     *
     * @param filterConfig FilterConfig
     * @throws ServletException 僼傿儖僞撪偱椺奜偑敪惗
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;

        // 僙僢僔儑儞儅僱乕僕儍偺愝掕
        try {
            this.sessionManager = SessionManager.getSessionManager();
        } catch (SessionManagerException e) {
            throw new ServletException(e.getMessage(), e);
        }

        // 僙僢僔儑儞僾儘僷僥傿僴儞僪儔偺愝掕
        this.sessionHandler = this.sessionManager.getSessionPropertyHandler();
    }

    /**
     * 儕僋僄僗僩偺僙僢僔儑儞娗棟傪偟傑偡丅
     * SessionFactory傪棙梡偟側偄応崌丄埲壓偺忦審偑偡傋偰枮偨偝傟傞偲偒僙僢僔儑儞偑宲懕偟偰偄傞傕偺偲傒側偝傟傑偡丅
     * <UL>
     * <LI>僙僢僔儑儞偑懚嵼偡傞
     * </UL>
     * SessionFactory傪棙梡偡傞応崌丄埲壓偺忦審偑偡傋偰枮偨偝傟傞偲偒僙僢僔儑儞偑宲懕偟偰偄傞傕偺偲傒側偝傟傑偡丅
     * <UL>
     * <LI><CODE>session</CODE>偵{@link SessionPropertyHandler#getLoginUserIDKey()}偱巜掕偝傟偨懏惈乮儘僌僀儞儐乕僓ID乯偑搊榐偝傟偰偄傞
     * <LI><CODE>session</CODE>偵{@link SessionPropertyHandler#getLoginGroupIDKey()}偱巜掕偝傟偨懏惈乮儘僌僀儞僌儖乕僾ID乯偑搊榐偝傟偰偄傞
     * </UL>
     *
     * @param servletRequest 儕僋僄僗僩
     * @param servletResponse 儗僗億儞僗
     * @param filterChain 僼傿儖僞
     * @throws IOException 僼傿儖僞張棟幚峴帪偵IOException偑敪惗
     * @throws ServletException 僼傿儖僞張棟幚峴帪偵ServletException偑敪惗
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        SessionObject session = null;
        HttpSession httpSession = null;
        boolean isSessionTimeOut = true;
        String errorPage = null;

        // 僙僢僔儑儞僆僽僕僃僋僩傪儕僋僄僗僩傪傕偲偵庢摼丒惗惉偡傞
        try {
            session = this.sessionManager.getSessionObject(servletRequest);
        } catch (SessionPropertyException e) {
            throw new ServletException(e.getMessage(), e);
        } catch (SessionFactoryException e) {
            throw new ServletException(e.getMessage(), e);
        }

        if (session == null) {
            // 儘僌僀儞僙僢僔儑儞偺堐帩忬嫷傪僠僃僢僋
            httpSession = ((HttpServletRequest)servletRequest).getSession(false);
            try {
                isSessionTimeOut = SessionFilterHelper.isSessionTimeOut(httpSession);
            } catch (SessionManagerException e) {
                throw new ServletException(e.getMessage(), e);
            } catch (SessionPropertyException e) {
                throw new ServletException(e.getMessage(), e);
            } catch (SessionObjectException e) {
                throw new ServletException(e.getMessage(), e);
            }
        } else {
            // 廬棃偺僙僢僔儑儞偺堐帩忬嫷僠僃僢僋乮屳姺梡乯
            // 儘僌僀儞僙僢僔儑儞偺堐帩忬嫷傪僠僃僢僋
            try {
                isSessionTimeOut = SessionFilterHelper.isSessionTimeOut(session);
            } catch (SessionManagerException e) {
                throw new ServletException(e.getMessage(), e);
            } catch (SessionPropertyException e) {
                throw new ServletException(e.getMessage(), e);
            } catch (SessionObjectException e) {
                throw new ServletException(e.getMessage(), e);
            }
        }
        if (isSessionTimeOut) {
            // 僙僢僔儑儞僞僀儉傾僂僩偺応崌僙僢僔儑儞僄儔乕儁乕僕偵慗堏
            try {
                errorPage = this.sessionHandler.getSessionErrorPagePath();
            } catch (SessionPropertyException e) {
                throw new ServletException(e.getMessage(), e);
            }
            this.filterConfig.getServletContext().getRequestDispatcher(errorPage).forward(servletRequest, servletResponse);
        } else {
            // 僙僢僔儑儞偑懕偄偰偄傞応崌偺傒師偺僼傿儖僞張棟
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    /**
     * 僼傿儖僞傪攋婞偟傑偡丅
     */
    public void destroy() {
        this.filterConfig = null;
        this.sessionManager = null;
        this.sessionHandler = null;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -