添加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 
    }

}

 

转载于:https://www.cnblogs.com/ldbangel/p/6094573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值