private String getPasswordForUser(Connection conn, String username) throws SQLException { PreparedStatement ps = null; ResultSet rs = null; String password = null; try { ps = conn.prepareStatement(authenticationQuery); ps.setString(1, username); rs = ps.executeQuery(); boolean foundResult = false; while (rs.next()) { if (foundResult) { throw new AuthenticationException( "More than one user row found for user [" + username + "]. Usernames must be unique."); } password = rs.getString(1); foundResult = true; } } finally { JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(ps); } return password; }
/** ��֤�ص�����, ��¼ʱ����. */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { CaptchaUsernamePasswordToken token = (CaptchaUsernamePasswordToken) authcToken; String parm = token.getCaptcha(); try { if (!imageCaptchaService.validateResponseForID( SecurityUtils.getSubject().getSession().getId().toString(), parm)) { throw new IncorrectCaptchaException("��֤�����"); } } catch (Exception e) { throw new IncorrectCaptchaException("��֤�����"); } String username = token.getUsername(); if (username == null) { throw new AccountException("Null usernames are not allowed by this realm."); } Connection conn = null; AuthenticationInfo info = null; try { conn = dataSource.getConnection(); String password = getPasswordForUser(conn, username); if (password == null) { throw new UnknownAccountException("No account found for user [" + username + "]"); } SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, getName()); simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(username)); info = simpleAuthenticationInfo; } catch (SQLException e) { final String message = "There was a SQL error while authenticating user [" + username + "]"; if (log.isErrorEnabled()) { log.error(message, e); } throw new AuthenticationException(message, e); } finally { JdbcUtils.closeConnection(conn); } return info; }
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { if (principals == null) { throw new AuthorizationException("PrincipalCollection method argument cannot be null."); } try { User user = (User) getAvailablePrincipal(principals); Connection conn = null; Set<String> roleNames = null; Set<String> permissions = null; try { conn = dataSource.getConnection(); // Retrieve roles and permissions from database roleNames = getRoleNamesForUser(conn, user.getId()); if (permissionsLookupEnabled) { permissions = getPermissions(conn, user.getId(), roleNames); } } catch (SQLException e) { final String message = "There was a SQL error while authorizing user [" + user.getId() + "]"; if (logger.isErrorEnabled()) { logger.error(message, e); } // Rethrow any SQL errors as an authorization exception throw new AuthorizationException(message, e); } finally { JdbcUtils.closeConnection(conn); } SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames); info.setStringPermissions(permissions); return info; } catch (Exception ex) { logger.error("Unable to get authorization info"); } return null; }