@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { boolean result = false; Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String) headerNames.nextElement(); if ("user-agent".equals(headerName.toLowerCase())) { if ("android".equals(request.getHeader(headerName).toLowerCase())) { return true; } } } // 开发环境拦截策略 if (Constants.ENV_DEVELOPMENT.equals( com.lefthand.comm.context.SystemContextHolder.getEnvironment())) { result = this.preHandleDevelopment(request, response, handler); } // 测试环境拦截策略 if (Constants.ENV_TEST.equals(com.lefthand.comm.context.SystemContextHolder.getEnvironment())) { result = this.preHandleTest(request, response, handler); } // 生产环境拦截策略 if (Constants.ENV_PRODUCTION.equals( com.lefthand.comm.context.SystemContextHolder.getEnvironment())) { result = this.preHandleProduction(request, response, handler); } return result; }
private boolean preHandleDevelopment( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { boolean result = false; String ctx = request.getContextPath(); String uri = request.getRequestURI(); // 判断拦截器排除的请求集合中是否包含当前请求 if (excludes != null) { for (String exclude : excludes) { // 如果包含当前请求, 则不拦截 if (uri.startsWith(ctx + exclude)) { result = true; break; } } if (result) { logger.debug("身份认证拦截器排除的请求: {}", uri); return result; } } // 判断是否通过身份认证, 且存在会话信息 com.lefthand.comm.domain.Handleable session = com.lefthand.comm.context.SecurityContextHolder.getHandler(); // 没有会话信息, 则重定向到系统登录页 if (session == null) { logger.debug("身份认证拦截器拦截的请求: {}", uri); response.sendRedirect(ctx + com.lefthand.comm.context.SystemContextHolder.getLogin()); return false; } return true; }