日期:2014-05-19 浏览次数:20811 次
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { /** * 1,doFilter方法的第一个参数为ServletRequest对象。此对象给过滤器提供了对进入的信息(包括 * 表单数据、cookie和HTTP请求头)的完全访问。第二个参数为ServletResponse,通常在简单的过 * 滤器中忽略此参数。最后一个参数为FilterChain,此参数用来调用servlet或JSP页。 */ HttpServletRequest request = (HttpServletRequest) servletRequest; /** * 如果处理HTTP请求,并且需要访问诸如getHeader或getCookies等在ServletRequest中 * 无法得到的方法,就要把此request对象构造成HttpServletRequest */ HttpServletResponse response = (HttpServletResponse) servletResponse; HttpSession session = request.getSession(false); String currentURL = request.getRequestURI(); // 取得根目录所对应的绝对路径: System.out.println(currentURL); Iterator<Resources> iter = securityResourcesService .getSystemResources().iterator(); if(session!=null){ if(currentURL.indexOf("Login")<0&¤tURL.indexOf("validateCode")<0){ [color=#FF0000]session.setAttribute(Procedure.URL_BLACK, currentURL);[/color] } } while (iter.hasNext()) { Resources source = iter.next(); // 路径格式 /onionPortal/adver/ if (Pattern.matches(source.getUrl() + "\\S*\\s*", currentURL)) { System.out.println("拦截认证请求成功!"); if (session == null || session.getAttribute("user") == null) { System.out.println("----sessionKey:"+Procedure.LAST_LOGIN_IP_SESSION_KEY); [color=#FF0000]session.setAttribute(Procedure.LAST_LOGIN_IP_SESSION_KEY,getIpAddrByRequest(request));[/color] // *用户登录以后需手动添加session System.out.println("request.getContextPath()=" + request.getContextPath()); response.sendRedirect(request.getContextPath() + "/userLogin/middlePage.html"); return; } else { Usertable users = (Usertable) session.getAttribute("user"); Iterator<Role> sourceRole = source.getRole().iterator(); re: while (sourceRole.hasNext()) { Role role = sourceRole.next(); Iterator<Role> usrRoleiter = users.getRoles() .iterator(); while (usrRoleiter.hasNext()) { if (role.getId() == usrRoleiter.next().getId()) {//如果匹配上则跳出循环 break re; } } //跳到登录页面 response.sendRedirect(request.getContextPath()+ "/userLogin/middlePage.html"); } } } break; } // 加入filter链继续向下执行 filterChain.doFilter(request, response); /** * 调用FilterChain对象的doFilter方法。Filter接口的doFilter方法取一个FilterChain对象作 为它 * 的一个参数。在调用此对象的doFilter方法时,激活下一个相关的过滤器。如果没有另 * 一个过滤器与servlet或JSP页面关联,则servlet或JSP页面被激活。 */ }