当前位置: 首页 > 知识库问答 >
问题:

使用JGit密钥安全地访问Git存储库

贡可人
2023-03-14

我正在使用JGit访问远程Git回购,我需要为此使用SSH。JGit使用JSch提供安全访问。但是,我不确定如何设置JGit的密钥文件和知道主机文件。我所尝试的如下。

创建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: 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

我已经添加了git。测验我的/etc/hosts文件的com条目。我使用相同的代码访问带有http url的git repo,因此代码运行良好。这是关键的处理部分是失败的。你知道怎么处理吗?

共有3个答案

史昀
2023-03-14

设法找到了问题所在。服务器端的公钥的名称与通常的id_rsa不同。pub,而我这边的私钥是id_rsa。JSch期望在默认情况下,公钥与私钥具有相同的名称加上。酒吧后缀。使用具有公共名称的密钥对(例如:private=key_1和public=key_1.pub)可以解决这个问题。

钱黎明
2023-03-14

Jsch SEEMS与哈希格式的已知_hosts文件不同——它必须符合以下生成的格式:

ssh-keyscan-t rsa主机名

例如

<hostname> ssh-rsa <longstring/longstring>

不是:

 |1|<hashed hostname>= ecdsa-sha2-nistp256 <hashed fingerprint>=

冀俊良
2023-03-14

您需要在自定义工厂类中重写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.removeAllId相很重要;没有它似乎无法工作。

警告:我用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
    }
}
 类似资料:
  • 问题内容: 我正在使用JGit访问远程Git存储库,并且需要使用SSH。JGit使用JSch提供安全访问。但是,我不确定如何为JGit设置密钥文件和已知主机文件。我尝试过如下。 通过子类化JSchConfigSessionFactory创建了SshSessionFactory的自定义配置: 在我访问远程Git存储库的课程中,执行了以下操作: 我不知道如何将此JSch对象与JGit关联,以便它可以成

  • 我在Powershell中编写了一个程序,该程序在Azure Functions应用程序中按计划运行。为了避免硬编码的凭据,我创建了一个Azure密钥库来存储秘密。我在Azure函数中创建了一个托管标识,在Azure Key Vault中创建了秘密,然后在Azure函数中创建了应用程序设置,并使用指向Azure Key Vault中秘密的URL。程序引用应用程序机密(APPSETTING),并按照

  • 问题内容: 我想知道如何在Android中安全存储 加密密钥 吗?保护加密和秘密密钥的最佳方案是什么? 问题答案: 根据您的评论,您需要使用适用于当前Android版本和旧Android版本的本地密钥对数据进行加密 Android Keystore 旨在生成和保护您的密钥。但是它不适用于18级以下的API级别,并且在API 23级之前有一些限制。 您将需要一个随机对称加密密钥,例如AES。AES密

  • 我在工作中被要求保护保存在Mysql数据库中的敏感数据。这个数据库包含几个表,其中一个表中的关键数据只能使用Django中的API来访问。对于这个API,只有一定数量的人可以访问它,因此他们将是唯一能够访问这个表中的数据的人。 因此,目前的问题是每个人都可以访问数据库和该表,因此我们决定使用AES在AES_ENCRYPT()和AES_DECRYPT()函数的帮助下加密该表中的所有数据(根据http

  • 在flutter应用程序中存储key.jks文件对于flutter应用程序发布是安全的吗? 存储库密码、keyPassword、keyAlias 我的key.properties文件:

  • 我正在开发一个将文件上传到Azure存储的.NET应用程序。我正在利用https://azure.microsoft.com/en-us/documentation/articles/storage-encrypt-decrypt-blobs-key-vault/教程中所做的客户端加密 应用程序工作,即,我可以成功地上传一个加密的blob到一个选定的存储帐户和容器。 但是,我对RSA密钥的安全性有