【问题标题】:Svn Authentication username, password and URL using Svn Kit使用 Svn Kit 验证用户名、密码和 URL
【发布时间】:2014-03-24 19:49:59
【问题描述】:

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

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

我当前的代码

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

【问题讨论】:

  • 你可以使用 SVNLogClient 代替 SVNUpdateClient
  • 你能详细说明@FredericClose
  • 使用 SVNLogClient 您将使用相同的身份验证机制,但不会强制签出文件以验证登录名/密码。
  • 也可以使用SVNRepository#testConnection方法。
  • 该类中的#testConnection 方法在哪里? @DmitryPavlenko

标签: java svn authentication svnkit


【解决方案1】:

您可以使用以下代码:

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() 只是最简单的一种。

【讨论】:

  • 使用 DefaultSVNAuthenticationManager 时,即使我在 Windows 机器中输入错误的密码,代码也会继续执行。在 linux 机器上,我收到此 URL“issues.tmatesoft.com/issue/SVNKIT-290”中提到的错误您的代码会解决这个问题吗? ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2018-05-26
  • 2013-12-26
相关资源
最近更新 更多