过滤器实现权限控制

过滤器实现权限控制

关键思路就是首先你要确定你要给哪些页面放行,就给这部分dofilter()

例如:

HttpServletRequest req=(HttpServletRequest)request;
        HttpServletResponse resp=(HttpServletResponse)response;
        String servletPath=req.getServletPath();
        System.out.println(servletPath);
        HttpSession session =req.getSession();
        String flag =(String) session.getAttribute("flag");
        if(servletPath!=null&&servletPath.equals("/16/index.jsp")||
                servletPath.equals("/16/login.jsp")||servletPath.equals("/loginServlet16")) {
        // pass the request along the filter chain
        chain.doFilter(request, response);
        }else{
            if(flag!=null&&flag.equals("success")){
            chain.doFilter(request, response);
        }else {
            session.setAttribute("return_url", req.getContextPath()+servletPath);
            RequestDispatcher rd=req.getRequestDispatcher("/16/login.jsp");
            rd.forward(req, resp);
        }
    }

比如:我现在有三个页面

hello.jsp

index.jsp

login.jsp

那么一开始都是进入index.jsp页面,但是只有登录成功者才能进入hello.jsp

所以除了hello.jsp,其他都直接放行

那么接下来就是给登录成功状态的用户放行

登录以后,在session中记录了登录状态为success,那么所有登录成功状态的用户也将被放行

其余没登录或者登录失败的用户则无法被放行,他们访问hello.jsp页面时则会被直接跳转到login.jsp页面,然后登录成功后直接进入将要访问的hello.jsp页面

扫描二维码关注公众号,回复: 612593 查看本文章

备注:一定别忘了给servlet放行

猜你喜欢

转载自www.cnblogs.com/xtuxiongda/p/9028980.html