使用 Svn 套件的 Svn 身份验证用户名、密码和 URL



如何在不签出项目的情况下使用用户名和密码对svn url进行身份验证

现在我调用 doCheckout() 方法来进行身份验证。我不想下载到本地星星。我只想进行身份验证。

我当前的代码

public class SvnAuth {
    private static SVNClientManager ourClientManager;
    public boolean svnAuthentication(String svnLocation, String svnUserName,
        String svnPassword, File file) throws SVNException {
        DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
        ourClientManager = SVNClientManager.newInstance(options, svnUserName,
                svnPassword);
        SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
        updateClient.setIgnoreExternals(false);
        SVNRevision rev = SVNRevision.HEAD;
        try {
            long revision1 = updateClient.doCheckout(
                    SVNURL.parseURIEncoded(svnLocation), file, rev, rev, true);
        } catch (SVNException e) {
            SVNErrorMessage svnErrorMessage = e.getErrorMessage();
            throw new SVNException(svnErrorMessage);
        }
        return true;
    }
}

我使用的SVNKit版本是1.7.4-v1

您可以使用以下代码:

private boolean checkPassword(SVNURL url, String user, String password) throws SVNException {
    SVNRepository svnRepository = SVNRepositoryFactory.create(url);
    try {
        svnRepository.setAuthenticationManager(new BasicAuthenticationManager(user, password));
        svnRepository.testConnection();
        return true;
    } catch (SVNException e) {
        if (e.getErrorMessage().getErrorCode() == SVNErrorCode.RA_NOT_AUTHORIZED) {
            return false;
        } else {
            throw e;
        }
    } finally {
        svnRepository.closeSession();
    }
}

实际上,您可以使用任何从存储库中读取一些数据的方法代替svnRepository.testConnection(),testConnection()只是最简单的方法。

最新更新