简体   繁体   English

Svn使用Svn Kit验证用户名,密码和URL

[英]Svn Authentication username, password and URL using Svn Kit

How to do authentication of a svn url with username and password without checking out the project 如何在不签出项目的情况下使用用户名和密码对svn url进行身份验证

Now I am calling the doCheckout() method to authenticate. 现在我调用doCheckout()方法进行身份验证。 I don't want to download to local starge. 我不想下载到本地的starge。 I just want to do authentication. 我只是想做身份验证。

My current code 我目前的代码

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 version I am using is 1.7.4-v1 我使用的SVNKit版本是1.7.4-v1

You can use the following code: 您可以使用以下代码:

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();
    }
}

Actually, instead of svnRepository.testConnection() you can use any method that reads some data from the repository, testConnection() is just the simplest one. 实际上,您可以使用从存储库中读取某些数据的任何方法来代替svnRepository.testConnection(),而testConnection()只是最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM