zoukankan      html  css  js  c++  java
  • filter过滤器(权限过滤)

    /**
     * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面redirectURL
     *  如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath notCheckURLList
     * 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath
     */

    public class RightFilter implements Filter {
     protected FilterConfig filterConfig = null;
     private String redirectURL = null;
     private Set<String> notCheckURLList = new HashSet<String>();
    
     @Override
     public void destroy() {
      // TODO Auto-generated method stub
      notCheckURLList.clear();
     }
     @SuppressWarnings({ "unchecked", "unchecked" })
     @Override
     public void doFilter(ServletRequest req, ServletResponse resp,
       FilterChain chain) throws IOException, ServletException {
      HttpServletResponse response = (HttpServletResponse) resp;
      HttpServletRequest request = (HttpServletRequest) req;
      HttpSession session = request.getSession();
      //
      if(checkRequestURIIntNotFilterList(request)){
       if(request.getRequestURI().endsWith("/platformindex.jsp") && session.getAttribute("user") == null){
        response.sendRedirect(request.getContextPath() + redirectURL);
           return;
       }
       chain.doFilter(req, resp);
       return;
      }else{
       if(session.getAttribute("user") == null){
        response.sendRedirect(request.getContextPath() + redirectURL);
           return;
       }else{
        String strCurrentPath =request.getContextPath() + request.getServletPath()
              + (request.getPathInfo() == null ? "" : request.getPathInfo());
        boolean isHaveRight = false;
        //询问访问的是否是用户菜单
        List<SysModule> userMenuist=(List<SysModule>) session.getAttribute("userMenu");
        if(userMenuist!=null && userMenuist.size()>0){
         for(SysModule tempModel  : userMenuist){
          if(tempModel.getModuleLocation().equals(strCurrentPath)){
           isHaveRight=true;
           break;
          }
         }
         if(isHaveRight){
          chain.doFilter(req, resp);
          return;
         }
        }
        //系统权限菜单
        List<SysRight> rightsList = (List<SysRight>) session.getAttribute("userRight");
        if(rightsList!=null && rightsList.size()>0){
         for(SysRight tempRight :rightsList){
          if(tempRight.getSysModule().getModuleLocation().equals(strCurrentPath)){
           isHaveRight=true;
           break;
          }
         }
         if(!isHaveRight){
          //无权限
          response.sendRedirect(request.getContextPath() + redirectURL);
             return;
         }else{
          chain.doFilter(req, resp);
          return;
         }
        }
       }
      }
     }
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
      // TODO Auto-generated method stub
      this.filterConfig = filterConfig;
         redirectURL = filterConfig.getInitParameter("redirectURL");
         String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");
         if (notCheckURLListStr != null) {
             String[] params = notCheckURLListStr.split(",");
             for (int i = 0; i < params.length; i++) {
               if(params[i].trim().endsWith(".jsp")){
                 notCheckURLList.add(params[i].trim());
              }else{
               makeNotCheckURLList(params[i].trim());
              }
             }
          }
     }
     private void makeNotCheckURLList(String strDirPath){
      String strFilePath = new RightFilter().getClass().getResource("/").getPath().replaceFirst("/", "").replaceAll("WEB-INF/classes/", "")+strDirPath;
      File notCheckfile = new File(strFilePath);
      File[] arrayFile = notCheckfile.listFiles();
      if(arrayFile==null){
       return;
      }
      for (int i = 0; i < arrayFile.length; i++) {
       if(arrayFile[i].isFile() && arrayFile[i].getName().endsWith(".jsp")){
        notCheckURLList.add(strDirPath+arrayFile[i].getName());
       }
      }
     }
     private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
         String uri = request.getServletPath()
             + (request.getPathInfo() == null ? "" : request.getPathInfo());
         return notCheckURLList.contains(uri);
       }
    
    }

    ----------

    配置文件:

    <filter>
        <filter-name>rightFilter</filter-name>
        <filter-class>cn.RightFilter</filter-class>
        <init-param>
          <param-name>redirectURL</param-name>
          <param-value>/page/login.jsp</param-value>   <!--   定位到login界面  -->
        </init-param>
        <init-param>
          <param-name>notCheckURLList</param-name><!--  不检查用户访问路径  -->
          <param-value>/page/</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>rightFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
      </filter-mapping>
    public class CharacterEncodingFilter implements Filter {
    
        private String encoding = "UTF-8";
    
        public void init(FilterConfig filterConfig) throws ServletException {
            String encoding = filterConfig.getInitParameter("encoding");
            if (encoding != null) {
                this.encoding = encoding;
            }
        }
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
                throws IOException, ServletException {
            if (encoding != null) {
                servletRequest.setCharacterEncoding(this.encoding);
            }
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            encoding = null;
        }
    
    
    
    }
  • 相关阅读:
    Oracle Dataguard管理命令(logical standby)
    RAC 主库配置单实例ADG
    基于参数shared_pool_reserved_size进一步理解共享池shared pool原理
    线性表的本质和操作
    类族结构的进化
    顶层父类的创建
    异常类的构建——5个子类构建
    异常类的构建——顶层父类Exception的实现
    智能指针示例
    泛型编程简介
  • 原文地址:https://www.cnblogs.com/lbangel/p/3045847.html
Copyright © 2011-2022 走看看