我正在使用JGit访问远程Git存储库,并且我需要使用SSH来访问它。JGit使用JSch提供安全访问。但是,我不确定如何为JGit设置密钥文件和knows hosts文件。我尝试的内容如下。
通过将JSchConfigSessionFactory子类化,使用创建了SshSessionFactory的自定义配置
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: git@git.test.com: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: git@git.test.com: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
我已经在我的/etc/hosts
文件中添加了git.test.com条目。我使用了相同的代码来访问带有http url的git repo,所以代码运行得很好。这是关键的处理部分正在失败。你知道怎么处理这件事吗?
发布于 2012-12-05 07:55:45
设法找到了问题所在。服务器端的公钥与通常的id_rsa.pub不同,而我端的私钥是id_rsa。默认情况下,JSch期望公钥具有与私钥相同的名称加上.pub后缀。使用具有公共名称的密钥对(例如: private = key_1和public = key_1.pub)可以解决这个问题。
发布于 2013-11-12 13:53:54
您需要在自定义工厂类中覆盖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
}
}
发布于 2016-05-11 17:30:30
Jsch不喜欢散列格式的known_hosts文件--它必须符合以下生成的格式:
ssh-keyscan -t rsa hostname >> ~/.ssh/known_hosts
例如:
<hostname> ssh-rsa <longstring/longstring>
不是:
|1|<hashed hostname>= ecdsa-sha2-nistp256 <hashed fingerprint>=
https://stackoverflow.com/questions/13686643
复制相似问题