•public class CheckLoginFilter   
• implements Filter   
•{   
•    protected FilterConfig filterConfig = null;   
•    private String redirectURL = null;   
•    private List notCheckURLList = new ArrayList();   
•    private String sessionKey = null;   
•  
• public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException   
• {   
•  HttpServletRequest request = (HttpServletRequest) servletRequest;   
•  HttpServletResponse response = (HttpServletResponse) servletResponse;   
•  
•   HttpSession session = request.getSession();   
•  if(sessionKey == null)   
•  {   
•   filterChain.doFilter(request, response);   
•   return;   
•  }   
•  if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)   
•  {   
•   response.sendRedirect(request.getContextPath() + redirectURL);   
•   return;   
•  }   
•  filterChain.doFilter(servletRequest, servletResponse);   
• }   
•  
• public void destroy()   
• {   
•  notCheckURLList.clear();   
• }   
•  
• private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)   
• {   
•  String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());   
•  return notCheckURLList.contains(uri);   
• }   
•  
• public void init(FilterConfig filterConfig) throws ServletException   
• {   
•  this.filterConfig = filterConfig;   
•  redirectURL = filterConfig.getInitParameter("redirectURL");   
•  sessionKey = filterConfig.getInitParameter("checkSessionKey");   
•  
•  String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");   
•  
•  if(notCheckURLListStr != null)   
•  {   
•   StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");   
•   notCheckURLList.clear();   
•   while(st.hasMoreTokens())   
•   {   
•    notCheckURLList.add(st.nextToken());   
•   }   
•  }   
• }   
•} 
求专家帮我把这个过滤器方法中一些主要代码加下注释,IF判断条件也帮忙注释一下,在下不胜感激!

解决方案 »

  1.   

    //这是一个检查请求安全权限的Filter,可以配置。起作用时会将不符合条件的请求重定向。
    •public class CheckLoginFilter  
    • implements Filter  //过滤器都要实现该接口
    •{  
    • protected FilterConfig filterConfig = null;  //用于保存配置信息
    • private String redirectURL = null;  
    • private List notCheckURLList = new ArrayList();  //可以免于处理的URL清单
    • private String sessionKey = null;  //若null则过滤器不起作用,希望启动本过滤器的时候要设一个非null值
    •  
    • public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException  
    • {  
    • HttpServletRequest request = (HttpServletRequest) servletRequest;  //类型转换,方便后面调用getSession等方法
    • HttpServletResponse response = (HttpServletResponse) servletResponse;  //类型转换
    •  
    • HttpSession session = request.getSession();  //取当前请求对应的会话
    • if(sessionKey == null)  
    • {  
    • filterChain.doFilter(request, response);  //放行
    • return;  
    • }  
    • if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)  
    • {  //如果本URL必须处理,同时其会话中又没有sessionKey记号,则拦截这些请求,重定向到指定URL
    • response.sendRedirect(request.getContextPath() + redirectURL);  
    • return;  
    • }  
    • filterChain.doFilter(servletRequest, servletResponse);  //如果上面的检查通过,则放行
    • }  
    •  
    • public void destroy()  
    • {  
    • notCheckURLList.clear();  
    • }  
    •  
    • private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)  //判断当前请求是否属于可放行之列
    • {  
    • String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());  
    • return notCheckURLList.contains(uri);  
    • }  
    •  
    • public void init(FilterConfig filterConfig) throws ServletException  
    • {  
    • this.filterConfig = filterConfig;  
    • redirectURL = filterConfig.getInitParameter("redirectURL");  //将重定向目标URL配置信息读入
    • sessionKey = filterConfig.getInitParameter("checkSessionKey");  //将是否过滤的配置信息读入
    •  
    • String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");  //将可忽略的URL配置信息读入
    •  
    • if(notCheckURLListStr != null)  
    • {  
    • StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");  //可忽略的URL配置时用;分隔
    • notCheckURLList.clear();  
    • while(st.hasMoreTokens())  
    • {  
    • notCheckURLList.add(st.nextToken());  
    • }  
    • }  
    • }  
    •}  
    程序不短。
    希望能帮到你。