Reputation: 97
How to do authentication of a svn url with username and password without checking out the project
Now I am calling the doCheckout() method to authenticate. I don't want to download to local 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
Upvotes: 0
Views: 3637
Reputation: 8978
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.
Upvotes: 2