2014-02-22 46 views
0

如何在沒有檢查出項目SVN認證的用戶名,密碼和URL使用SVN套件

現在我調用doCheckout()身份驗證方法做用戶名和密碼SVN URL的認證。我不想下載到本地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

+0

您可以使用SVNLogClient代替SVNUpdateClient –

+0

您能否詳細說明@FredericClose – BhajjiMaga

+0

使用SVNLogClient,您將使用相同的身份驗證機制,但不會強制檢出文件以驗證登錄名/密碼。 –

回答

2

您可以使用下面的代碼:

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()只是最簡單的一個。

+0

使用DefaultSVNAuthenticationManager時,即使在Windows機器中輸入錯誤的密碼時,代碼仍會繼續執行。在Linux機器上,我收到了此URL中提到的錯誤「http://issues.tmatesoft.com/issue/SVNKIT-290」你的代碼能解決這個問題嗎? – Lucy