当前位置:编程学习 > JAVA >>

自用sessionfilter过滤器

package com.common;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter  {
    private FilterConfig filterConfig;

    public void setFilterConfig(final FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    public FilterConfig getFilterConfig() {
        return filterConfig;
    }

    public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        HttpSession session = req.getSession();
        String url = req.getRequestURI();
        req.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        if (session.getAttribute("loginUser") != null) {
                chain.doFilter(request, response);
        } else {
                if (url.indexOf("login.do") != -1 
                  || url.indexOf("index.do") != -1
                  || url.indexOf("initMainLdapTree.do") !=-1
                  || url.indexOf("indexMain.do") != -1
                  || url.indexOf("platData.do")!= -1
                  || url.indexOf("resDownLoad.do")!= -1
                  || url.indexOf("mainUrl.do")!= -1
                  || url.indexOf("showNewsOfOne.do")!= -1
                  || url.indexOf("showNewsList.do")!= -1
                  || url.indexOf("user_Infor.do")!= -1
                  || url.indexOf("initUserTree.do")!= -1
                  || url.indexOf("queryCenBusi.do")!=-1
                  || url.indexOf("queryUseBusiInMain.do")!=-1
                  || url.indexOf("queryBusiInforForMain.do")!=-1
                  || url.indexOf("lineData.do")!=-1
                  || url.indexOf("queryLdapMainAttr.do") !=-1
                  || url.indexOf("downLoadXml.do")!=-1
                  || url.indexOf("resDown.do")!=-1
                  || url.indexOf("selectUrl.do")!=-1
                  || url.indexOf("initDepEmailTree.do")!=-1
                  ||url.indexOf("selDoorForm.do")!=-1
                  ||url.indexOf("selChaXun.do")!=-1
                  ||url.indexOf("selHeCha.do")!=-1
                  ||url.indexOf("selBiDui.do")!=-1
                  ||url.indexOf("shangBao.do")!=-1
                  ) {
                    chain.doFilter(request, response);
                } else {
                    request.getRequestDispatcher("/jsp/sessionError.jsp").forward(request, response);
                }
         }
    }
    public void destroy() {
        this.filterConfig = null;
    }
}

 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,