jdbcauthenticator.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 696 行 · 第 1/2 页

JAVA
696
字号
     String id;            id = sm.createSessionId(request);            if (updateCookie(user, id)) {       Cookie cookie = new Cookie("resinauthid", id);       cookie.setPath("/");       if (getCookieVersion() >= 0)         cookie.setVersion(getCookieVersion());       else         cookie.setVersion(sm.getCookieVersion());       if (_cookieDomain != null)         cookie.setDomain(_cookieDomain);       else if (getCookieDomain() != null)         cookie.setDomain(getCookieDomain());       else         cookie.setDomain(sm.getCookieDomain());        if (_cookieMaxAge > 0)         cookie.setMaxAge((int) (_cookieMaxAge / 1000L));       response.addCookie(cookie);     }   }  /**   * Authenticates the user given the request.   *   * @param username the user name for the login   * @param password the password for the login   *   * @return the authenticated user or null for a failure   */  public Principal loginImpl(String username, String password)    throws ServletException  {    Connection conn = null;    PreparedStatement stmt = null;    ResultSet rs = null;    try {      conn = _dataSource.getConnection();      stmt = conn.prepareStatement(_passwordQuery);      stmt.setString(1, username);      rs = stmt.executeQuery();      if (! rs.next()) {        if (log.isLoggable(Level.FINE))          log.fine("no such user:" + username);                return null;      }            String dbPassword = rs.getString(1);      if (dbPassword != null && dbPassword.equals(password)) {        return new CachingPrincipal(username);      }      else {        if (log.isLoggable(Level.FINE))          log.fine("mismatched password:" + username);                return null;      }    } catch (Exception e) {      e.printStackTrace();      throw new ServletException(e);    } finally {      try {        if (rs != null)          rs.close();      } catch (SQLException e) {      }      try {        if (stmt != null)          stmt.close();      } catch (SQLException e) {      }      try {        if (conn != null)          conn.close();      } catch (SQLException e) {      }    }  }    /**   * Returns the password for authenticators too lazy to calculate the   * digest.   */  protected String getDigestPassword(HttpServletRequest request,                                     HttpServletResponse response,                                     ServletContext application,                                     String username, String realm)    throws ServletException  {    Connection conn = null;    PreparedStatement stmt = null;    ResultSet rs = null;          try {      conn = _dataSource.getConnection();      stmt = conn.prepareStatement(_passwordQuery);      stmt.setString(1, username);      rs = stmt.executeQuery();      if (! rs.next()) {        if (log.isLoggable(Level.FINE))          log.fine("no such user:" + username);                return null;      }            String dbPassword = rs.getString(1);      return dbPassword;    } catch (Exception e) {      throw new ServletException(e);    } finally {      try {        if (rs != null)          rs.close();      } catch (SQLException e) {      }      try {        if (stmt != null)          stmt.close();      } catch (SQLException e) {      }      try {        if (conn != null)          conn.close();      } catch (SQLException e) {      }    }  }  protected Principal getUserPrincipalImpl(HttpServletRequest request,                                           ServletContext application)    throws ServletException  {    if (_cookieQuery == null)      return null;        Cookie cookie = null;        if (request instanceof CauchoRequest)      cookie = ((CauchoRequest) request).getCookie("resinauthid");    else {      Cookie []cookies = request.getCookies();      for (int i = 0; cookies != null && i < cookies.length; i++) {        if (cookies[i].getName().equals("resinauthid")) {          cookie = cookies[i];          break;        }      }    }    if (cookie == null)      return null;    return authenticateCookie(cookie.getValue());  }  /**   * Authenticate based on a cookie.   *   * @param cookieValue the value of the resin-auth cookie   *   * @return the user for the cookie.   */  public Principal authenticateCookie(String cookieValue)    throws ServletException  {    if (_cookieQuery == null)      return null;    Connection conn = null;    PreparedStatement stmt = null;    ResultSet rs = null;        try {      conn = _dataSource.getConnection();      stmt = conn.prepareStatement(_cookieQuery);      stmt.setString(1, cookieValue);      rs = stmt.executeQuery();      if (! rs.next())        return null;            String user = rs.getString(1);      if (user != null)        return new CachingPrincipal(user);      else        return null;    } catch (Exception e) {      throw new ServletException(e);    } finally {      try {        if (rs != null)          rs.close();      } catch (SQLException e) {      }      try {        if (stmt != null)          stmt.close();      } catch (SQLException e) {      }      try {        if (conn != null)          conn.close();      } catch (SQLException e) {      }    }  }  /**   * Associates a user with a persistent cookie.   *   * @param user the user for the cookie   * @param cookieValue the value of the resin-auth cookie   *   * @return true if the cookie value is valid, i.e. it's unique   */  public boolean updateCookie(Principal user, String cookieValue)  {    if (_cookieUpdate == null || user == null || cookieValue == null)      return true;        Connection conn = null;    PreparedStatement stmt = null;        try {      conn = _dataSource.getConnection();      stmt = conn.prepareStatement(_cookieUpdate);      stmt.setString(1, cookieValue);      stmt.setString(2, user.getName());      stmt.executeUpdate();    } catch (Exception e) {      log.log(Level.FINE, e.toString(), e);    } finally {      try {        if (stmt != null)          stmt.close();      } catch (SQLException e) {      }      try {        if (conn != null)          conn.close();      } catch (SQLException e) {      }    }    return true;  }  public boolean isUserInRole(HttpServletRequest request,                              HttpServletResponse response,                              ServletContext application,                              Principal principal, String role)  {    if (_roleQuery == null)      return principal != null && "user".equals(role);    else if (principal == null || role == null)      return false;    CachingPrincipal cachingPrincipal = null;    if (principal instanceof CachingPrincipal) {      cachingPrincipal = (CachingPrincipal) principal;      Boolean isInRole = cachingPrincipal.isInRole(role);      if (isInRole != null)	return isInRole.equals(Boolean.TRUE);    }    Connection conn = null;    PreparedStatement stmt = null;    ResultSet rs = null;          try {      conn = _dataSource.getConnection();      stmt = conn.prepareStatement(_roleQuery);      stmt.setString(1, principal.getName());      boolean inRole = false;            rs = stmt.executeQuery();      while (rs.next()) {        String dbRole = rs.getString(1);	if (cachingPrincipal != null)	  cachingPrincipal.addRole(dbRole);	        if (role.equals(dbRole))	  inRole = true;      }            return inRole;    } catch (Exception e) {      log.log(Level.FINE, e.toString(), e);            return false;    } finally {      try {        if (rs != null)          rs.close();      } catch (SQLException e) {      }      try {        if (stmt != null)          stmt.close();      } catch (SQLException e) {      }      try {        if (conn != null)          conn.close();      } catch (SQLException e) {      }    }  }  /**   * Logs the user out from the session.   *   * @param request the servlet request   */  public void logout(HttpServletRequest request,                     HttpServletResponse response,                     ServletContext application,                     Principal user)    throws ServletException  {    super.logout(request, response, application, user);    // null the cookie    if (_cookieLogout)      updateCookie(user, "");  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?