简体   繁体   English

SVN身份验证用户名,使用SVN套件的密码

[英]Svn Authentication username, password using Svn Kit

I'm trying to authenticate username, password entered by the user using svnkit 1.7.8. 我正在尝试验证用户使用svnkit 1.7.8输入的用户名和密码。 Currently I'm using "DefaultAuthenticationManager" in method "authenticate" to achieve this. 目前,我在方法“身份验证”中使用“ DefaultAuthenticationManager”来实现此目的。 The problem I'm facing is that even when i enter incorrect credentials the method doesn't throw any errors and continues with code execution. 我面临的问题是,即使我输入了错误的凭据,该方法也不会引发任何错误并继续执行代码。

My code.. 我的代码

 private void authenticate(String user, String password) throws SVNException {
    SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(baseUrl));
    ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(user, password);
    repository.setAuthenticationManager(authManager);
} 

Is this error due to usage of "DefaultAuthenticationManager" instead of "BasicAuthenticationManager" ?? 是由于使用“ DefaultAuthenticationManager”而不是“ BasicAuthenticationManager”导致此错误? Please Help 请帮忙

NOTE: 注意:

I'm checking out a svn directory from https url. 我正在从https url中检出svn目录。 I already have the working code for checking out a directory from SVN to local machine, Just need help with the Authentication part. 我已经有了用于从SVN检出目录到本地计算机的工作代码,只需要Authentication部分的帮助即可。

As I mentioned in my comment, you are throwing away the instance of the SVNRepository that you have initialized with the AuthmenticationManager . 正如我在评论中提到的那样,您将丢弃已使用AuthmenticationManager初始化的SVNRepository实例。

If you remember this answer that you commented on, and if you would have read the documentation , it's not hard to come up with something like this: 如果您记得您评论过的答案 ,并且已经阅读了文档 ,那么提出这样的内容就很容易了:

final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(user, password);
svnOperationFactory.setAuthenticationManager(authManager);

try {
    final SvnCheckout checkout = svnOperationFactory.createCheckout();
    checkout.setSingleTarget(SvnTarget.fromFile(workingCopyDirectory));
    checkout.setSource(SvnTarget.fromURL(url));
    //... other options
    checkout.run();
} finally {
    svnOperationFactory.dispose();
}

EDIT: If you really need to use the SVNRepository 编辑:如果您真的需要使用SVNRepository

just do: 做就是了:

SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(baseUrl));
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(user, password);
repository.setAuthenticationManager(authManager);

// now do any operation that you want with the *same* repository object
repository.checkout(..)

You can test with below code snippet: I was facing the same issue and this is how I fixed. 您可以使用以下代码段进行测试:我面临着同样的问题,这就是我如何解决的问题。

Used API : 使用的API:

svnkit-1.7.8.jar svnkit-1.7.8.jar

public boolean testSVNConnection(String svnURL,String svnUser,String svnPass){
 DAVRepositoryFactory.setup();
 String url="https://your_svn_host/path/";
 String name="username";
 String password="password";
 SVNRepository repository = null;
 try { 
     repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
     ISVNAuthenticationManager authManager = 
                  SVNWCUtil.createDefaultAuthenticationManager(name, password);
     repository.setAuthenticationManager(authManager);
     repository.testConnection();
     System.out.println("Connection done..");
     return true;
 } catch (SVNException e){
     System.out.println("Not connected");
     e.printStackTrace();
     return false;
 }

}

Just change your method signature as per requirement. 只需根据要求更改方法签名。

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

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