• 粗颗粒度权限控制(使用过滤器完成)


    分析:

    精确到Session的权限控制(判断Session是否存在)

    使用过滤器完成粗颗粒的权限控制,如果Session不存在就跳转到首页,如果存在可以通过URL链接访问到对应的操作。

    第一步:定义一个过滤器:

     1 public class SystemFilter implements Filter {
     2 
     3     /**web容器启动的时候,执行的方法*/
     4     //存放没有Session之前,需要放行的连接
     5     List<String> list = new ArrayList<String>();
     6     public void init(FilterConfig config) throws ServletException {
     7         list.add("/index.jsp");
     8         list.add("/image.jsp");
     9         list.add("/system/elecMenuAction_menuHome.do");
    10     }
    11     
    12     /**每次访问URL连接的时候,先执行过滤器的doFilter的方法*/
    13     public void doFilter(ServletRequest req, ServletResponse res,
    14             FilterChain chain) throws IOException, ServletException {
    15         HttpServletRequest request = (HttpServletRequest) req;
    16         HttpServletResponse response = (HttpServletResponse) res;
    17         //获取访问的连接地址
    18         String path = request.getServletPath();
    19         //在访问首页index.jsp页面之前,先从Cookie中获取name,password的值,并显示在页面上(完成记住我)
    20         this.forwordIndexPage(path,request);
    21         //如果访问的路径path包含在放行的List的存放的连接的时候,此时需要放行
    22         if(list.contains(path)){
    23             chain.doFilter(request, response);
    24             return;
    25         }
    26         //获取用户登录的Session
    27         ElecUser elecUser = (ElecUser)request.getSession().getAttribute("globle_user");
    28         //放行
    29         if(elecUser!=null){
    30             chain.doFilter(request, response);
    31             return;
    32         }
    33         //重定向到登录页面
    34         response.sendRedirect(request.getContextPath()+"/index.jsp");
    35     }
    36     
    37     /**销毁*/
    38     public void destroy() {
    39 
    40     }
    41 }

    第二步:在web容器中添加对应的过滤器:

     1 <!-- 自定义过滤器,要求添加到struts2过滤器的前面 -->
     2     <filter>
     3         <filter-name>SystemFilter</filter-name>
     4         <filter-class>cn.itcast.elec.util.SystemFilter</filter-class>
     5     </filter>
     6     <filter-mapping>
     7         <filter-name>SystemFilter</filter-name>
     8         <url-pattern>*.do</url-pattern>
     9         <url-pattern>*.jsp</url-pattern>
    10     </filter-mapping>

    总结:

    • 在过滤器中定义放行的连接,因为不是每个操作都会存在Session
    • 在过滤器中获取登录后存放的Session,如果Session不为空,则放行,即可以操作定义的业务功能,如果Session为空,则跳转到登录页面。
    • 控制访问的系统必须要存在Session

    注意:

      Session不应该在服务器一直不清空,如果Session过多,会导致Session压力大,系统变慢,于是要求10分钟如果不操作系统,将Session自动清空。在web.xml中配置

    1 <session-config>
    2     <session-timeout>10</session-timeout>
    3 </session-config>
  • 相关阅读:
    基于visual Studio2013解决面试题之1101差值最小
    基于visual Studio2013解决面试题之1009兄弟字符串
    基于visual Studio2013解决面试题之1007鸡蛋和篮子
    基于visual Studio2013解决面试题之1006判断升序
    4.16 删除重复记录
    4.9 当相应行存在时更新
    4.8 在表中编辑记录
    4.7 阻止对某几列插入
    4.6 一次向多个表中插入记录
    4.4 从一个表向另外的表中复制行
  • 原文地址:https://www.cnblogs.com/fuyiming/p/5815982.html
Copyright © 2020-2023  润新知