コード例 #1
0
 /**
  * Init session filter
  *
  * @param filterConfig A filter configuration object used by a servlet container to pass
  *     information to a filter during initialization.
  */
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
   // 初始化系统信息
   Constants.setSystemInfo(
       filterConfig.getServletContext().getRealPath(""),
       filterConfig.getServletContext().getContextPath(),
       filterConfig.getInitParameter("JS_Version"),
       null);
 }
コード例 #2
0
  /**
   * Do session filter
   *
   * @param request HttpServletRequest
   * @param response ServletResponse
   * @param chain A FilterChain is an object provided by the servlet container to the developer
   *     giving a view into the invocation chain of a filtered request for a resource. Filters use
   *     the FilterChain to invoke the next filter in the chain, or if the calling filter is the
   *     last filter in the chain, to invoke the resource at the end of the chain.
   */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    // Instance HttpServletRequest
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    // Get submit URL
    String url = httpServletRequest.getRequestURI();

    // Print URL
    if (logger.isDebugEnabled()) {
      logger.log(ComLogger.DEBUG, url);
    }

    // 过滤/page/ 路径.jsp|.html
    if (url.matches("^(\\w|\\W)*\\.(jsp|html)$")) {

      HttpSession session = httpServletRequest.getSession();
      UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_INFO_SESSION);

      if (userInfo == null || "".equals(userInfo)) {
        session.invalidate();

        if (logger.isDebugEnabled()) {
          logger.log(ComLogger.DEBUG, "SESSION过期,url:" + url);
        }

        if (url.matches("^.+(/page/|/main\\.html).*$")) {

          String QueryString = httpServletRequest.getQueryString();
          if ("hash=".equals(QueryString) || QueryString == null || "".equals(QueryString)) {
            QueryString = "";
          } else {
            QueryString = "?" + QueryString;
          }
          httpServletRequest.setAttribute(
              "url", url.substring(Constants.getProjectUrl().length() + 1) + QueryString);
          httpServletRequest
              .getRequestDispatcher("/login.jsp")
              .forward(httpServletRequest, response);

        } else {
          HttpServletResponse newResponse = (HttpServletResponse) response;
          newResponse.setHeader("sessiontimeout", "1");
          //					newResponse.setStatus(998);
          newResponse.setContentType("text/plain;charset=UTF-8");
          newResponse.getWriter().write("{success:false,msg:\"您的登陆信息已过期,请先登录。\"}");
        }

        return;
      }
    }
    chain.doFilter(httpServletRequest, response);
  }