添加filter过滤器 (静态资源放行)

里面加了没有登录的时候,操作时就需要登录

package com.quicksure.mobile.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
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;

import com.quicksure.mobile.entity.Userinfor;

public class LudiMobileFilter implements Filter {
    public void destroy() {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {         
        HttpServletRequest req = (HttpServletRequest) request; 
        HttpServletResponse resp = (HttpServletResponse) response; 
        HttpSession session = req.getSession();
        String path = req.getContextPath();
        String url = req.getRequestURI();
        Userinfor user = (Userinfor) session.getAttribute("loginUser");
        String conString = req.getHeader("REFERER"); //获取父url,这里当浏览器url选中,敲enter键时,conString就会为空了
        
        if("".equals(conString) || null==conString){                     
            String servletPath = req.getServletPath();
            if (servletPath.indexOf("index") > 0 || servletPath.indexOf("LoginUser") > 0||servletPath.indexOf("MP_verify_PPlSQIplLY8fbefx.txt")>0) {
                chain.doFilter(request, response);
            } else { // 当前请求url
                resp.sendRedirect(path + "/views/quicksure/jsp/index.jsp");
                session.removeAttribute("loginUser");
            } 
        }else{
            if(url.endsWith(".css")||url.endsWith(".js")||url.endsWith(".jpg")
                ||url.endsWith(".gif")||url.endsWith(".png")||url.endsWith("/index.jsp")
                ||url.endsWith("/LoginUser.jsp")||url.endsWith("getDptCode.do")
                ||url.endsWith("checkPhoneCode.do")||url.endsWith("userLogin.do")
                ||url.endsWith("phoneCheck.do")||url.endsWith("/registUser.jsp")||url.endsWith("registUser.do")
                ||url.endsWith("resetPassword.do")||url.endsWith("/resetPassword.jsp")||url.endsWith("goPaymentSucessPage.do")){
                chain.doFilter(request, response);
            }else{
                if (user == null ) {
                    // 跳转到登陆页面
                    req.getRequestDispatcher("/views/quicksure/jsp/LoginUser.jsp").forward(request, response);
                    //request.getRequestDispatcher().forward(request, response);
                    //resp.sendRedirect(path+"/views/quicksure/jsp/LoginUser.jsp");
                } else {
                    // 已经登陆,继续此次请求
                    chain.doFilter(request, response);
                }
            }
        }         
    }

    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub 
    }

}

 

posted @ 2016-11-23 17:39  编码龟  阅读(6900)  评论(0编辑  收藏  举报