相关疑难解决方法(0)

使用JGit键可以安全地访问Git存储库

我正在使用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) …
Run Code Online (Sandbox Code Playgroud)

java git ssh jsch jgit

13
推荐指数
3
解决办法
1万
查看次数

Spring Cloud Config无法使用ssh密钥克隆私有bitbucket存储库

我在Linux(arch)上,尝试使用ssh密钥在本教程之后使用私有bitbucket git存储库配置Spring Cloud Config ,但我不断收到错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is java.lang.IllegalStateException: Cannot
clone or checkout repository] with root cause com.jcraft.jsch.JSchException: Auth fail
Run Code Online (Sandbox Code Playgroud)

现在,根据教程,它应该工作:

如果您不使用HTTPS和用户凭据,当您将密钥存储在默认目录(〜/ .ssh)中并且uri指向SSH位置时,SSH也应该开箱即用,例如"git@github.com:配置/云配置".重要的是〜/ .ssh/known_hosts中的所有键都是"ssh-rsa"格式.不支持新的"ecdsa-sha2-nistp256"格式.使用JGit访问存储库,因此您在其上找到的任何文档都应该适用.HTTPS代理设置可以在〜/ .git/config中设置,也可以通过系统属性(-Dhttps.proxyHost和-Dhttps.proxyPort)以与任何其他JVM进程相同的方式设置.

我在〜/ .ssh文件夹中有一个名为bitbucket-rsa的私有ssh密钥,使用该命令创建ssh-keygen -t rsa -b 4096 -C "my-email@provider.com".公钥正确添加到Bitbucket,因为我能够从命令行克隆,拉出和推送存储库.私钥已添加到ssh-agent中,bitbucket.org存在于known_hosts文件中.

这是config-service项目中的bootstrap.yml:

spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: "git@bitbucket.org:TarekSaid/my-private-repo.git"
server:
  port: 8888
Run Code Online (Sandbox Code Playgroud)

使用带有用户名和密码的https有效,但我仍然更喜欢使用ssh密钥,我该如何使其工作?

java spring bitbucket ssh-keys spring-cloud-config

3
推荐指数
1
解决办法
6272
查看次数

标签 统计

java ×2

bitbucket ×1

git ×1

jgit ×1

jsch ×1

spring ×1

spring-cloud-config ×1

ssh ×1

ssh-keys ×1