zoukankan      html  css  js  c++  java
  • 过滤器实现权限控制

    过滤器实现权限控制

    关键思路就是首先你要确定你要给哪些页面放行,就给这部分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页面

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

  • 相关阅读:
    逆元
    和平委员会
    抢掠计划
    间谍网络
    hacker发展流程图 菜菜学习中
    程序员练级之路
    程序员练级之路
    程序员练级之路
    程序员练级之路
    南邮STITP 基于图挖掘的大规模动态交互网络热点区域识别及分布式处理 立项书
  • 原文地址:https://www.cnblogs.com/xtuxiongda/p/9028980.html
Copyright © 2011-2022 走看看