2012-12-03 26 views
13

我使用JGit來訪問遠程Git倉庫,我需要使用SSH。 JGit使用JSch來提供安全訪問。但是,我不確定如何爲JGit設置密鑰文件和知道主機文件。我試過的如下。與JGit一起使用密鑰來安全訪問Git存儲庫

創建的SshSessionFactory的定製配置,使用由子類JSchConfigSessionFactory

public class CustomJschConfigSessionFactory extends JschConfigSessionFactory { 
    @Override 
    protected void configure(OpenSshConfig.Host host, Session session) { 
     session.setConfig("StrictHostKeyChecking", "yes"); 
    } 
} 

在我訪問遠程GIT中回購類,做了以下內容:

CustomJschConfigSessionFactory jschConfigSessionFactory = new CustomJschConfigSessionFactory(); 

JSch jsch = new JSch(); 
try { 
    jsch.addIdentity(".ssh/id_rsa"); 
    jsch.setKnownHosts(".ssh/known_hosts"); 
} catch (JSchException e) { 
    e.printStackTrace(); 
} 
    SshSessionFactory.setInstance(jschConfigSessionFactory); 

我不能瞭解如何將此JSch對象與JGit關聯,以便它可以成功連接到遠程存儲庫。當我試着使用JGit克隆它,我得到以下異常:

org.eclipse.jgit.api.errors.TransportException: [email protected]:abc.org/test_repo.git: reject HostKey: git.test.com 
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137) 
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178) 
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125) 
at GitTest.cloneRepo(GitTest.java:109) 
at GitTest.main(GitTest.java:223) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 
Caused by: org.eclipse.jgit.errors.TransportException: [email protected]:abc.org/test_repo.git: reject HostKey: git.test.com 
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:142) 
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:121) 
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:248) 
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:147) 
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136) 
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122) 
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1104) 
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128) 
... 9 more 
Caused by: com.jcraft.jsch.JSchException: reject HostKey: git.test.com 
at com.jcraft.jsch.Session.checkHost(Session.java:748) 
at com.jcraft.jsch.Session.connect(Session.java:321) 
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116) 
... 16 more 

我已經加入了git.test.com進入到我的/etc/hosts文件。我使用相同的代碼來訪問一個http url的git repo,所以它的代碼工作正常。這是失敗的關鍵處理部分。任何想法如何處理這個?

回答

3

管理員發現問題。服務器端的公鑰與通常的id_rsa.pub不同,但我身邊的私鑰是id_rsa。 JSch默認預計公鑰與私鑰加上.pub後綴具有相同的名稱。使用具有通用名稱的密鑰對(例如:private = key_1和public = key_1.pub)可解決此問題。

11

你需要重寫getJSch方法在自定義工廠類:

class CustomConfigSessionFactory extends JschConfigSessionFactory 
{ 
    @Override 
    protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException { 
     JSch jsch = super.getJSch(hc, fs); 
     jsch.removeAllIdentity(); 
     jsch.addIdentity("/path/to/private/key"); 
     return jsch; 
    } 
} 

調用jsch.removeAllIdentity是很重要的;沒有它,它似乎就無法工作。

一個告誡:我在Scala中寫了上面的代碼,然後將它翻譯成Java,所以它可能不太正確。原Scala是如下:

class CustomConfigSessionFactory extends JschConfigSessionFactory 
{ 
    override protected def getJSch(hc : OpenSshConfig.Host, fs : FS) : JSch = 
    { 
     val jsch = super.getJSch(hc, fs) 
     jsch.removeAllIdentity() 
     jsch.addIdentity("/path/to/private/key") 
     jsch 
    } 
} 
+1

我的問題是,我需要能夠指定密碼私鑰,您可以添加另一個參數'addIdentity'或通過創建自己的實現'UserInfo'的類,然後將'Session'參數設置爲'CustomConfigSessionFactory'的'configure'方法以使用'UserInfo'。 – WhiteKnight

+2

特別感謝'jsch.removeAllIdentity()'提示! – Vincent

4

Jsch sesems不喜歡在散列一個known_hosts文件format--它必須符合所產生的格式:

ssh-keyscan -t rsa hostname >> ~/.ssh/known_hosts

例如

<hostname> ssh-rsa <longstring/longstring> 

不是:

|1|<hashed hostname>= ecdsa-sha2-nistp256 <hashed fingerprint>= 
相關問題