Izz*_*zza 13 java git ssh jsch jgit
我正在使用JGit访问远程Git仓库,我需要使用SSH.JGit使用JSch提供安全访问.但是,我不确定如何为JGit设置密钥文件和已知的hosts文件.我试过的内容如下.
使用子类化JSchConfigSessionFactory创建SshSessionFactory的自定义配置:
public class CustomJschConfigSessionFactory extends JschConfigSessionFactory {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
session.setConfig("StrictHostKeyChecking", "yes");
}
}
Run Code Online (Sandbox Code Playgroud)
在我访问远程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);
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何将此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
Run Code Online (Sandbox Code Playgroud)
我已将git.test.com条目添加到我的/etc/hosts文件中.我使用相同的代码来访问带有http url的git repo,因此代码运行正常.这是失败的关键处理部分.有关如何处理这个的任何想法?
pau*_*ies 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;
}
}
Run Code Online (Sandbox Code Playgroud)
通话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
}
}
Run Code Online (Sandbox Code Playgroud)
设法找到了问题。服务器端的公钥与通常的 id_rsa.pub 不同,而我端的私钥是 id_rsa。默认情况下,JSch 期望公钥与私钥同名并加上 .pub 后缀。使用具有通用名称的密钥对(例如:private = key_1 和 public = key_1.pub)可以解决该问题。
Jsch sesems不喜欢散列格式的known_hosts文件 - 它必须符合以下格式:
ssh-keyscan -t rsa hostname >> ~/.ssh/known_hosts
例如
<hostname> ssh-rsa <longstring/longstring>
Run Code Online (Sandbox Code Playgroud)
不:
|1|<hashed hostname>= ecdsa-sha2-nistp256 <hashed fingerprint>=
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12517 次 |
| 最近记录: |