Lucy
Lucy

Reputation: 1852

Svn Authentication username, password using Svn Kit

I'm trying to authenticate username, password entered by the user using svnkit 1.7.8. Currently I'm using "DefaultAuthenticationManager" in method "authenticate" to achieve this. 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" ?? Please Help

NOTE:

I'm checking out a svn directory from https url. I already have the working code for checking out a directory from SVN to local machine, Just need help with the Authentication part.

Upvotes: 1

Views: 3863

Answers (2)

MonsterJava
MonsterJava

Reputation: 423

You can test with below code snippet: I was facing the same issue and this is how I fixed.

Used API :

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.

Upvotes: 1

Asa
Asa

Reputation: 1729

As I mentioned in my comment, you are throwing away the instance of the SVNRepository that you have initialized with the AuthmenticationManager.

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

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(..)

Upvotes: 3

Related Questions