首页 文章

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

提问于
浏览
2

我在Linux(arch)上,尝试使用ssh密钥配置一个私有的bitbucket git存储库,在此tutorial之后配置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

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

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

我在名为bitbucket-rsa的〜/ .ssh文件夹中有一个私有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

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

1 回答

  • 9

    终于成功了!

    这个问题:How to use a custom ssh key location with Spring Cloud Config指出了我正确的方向 . 我调试了 JschConfigSessionFactory 类,发现当没有提供用户名和密码时,它会从 ~/.ssh/config 中的默认配置文件中获取配置 .

    因此,我所要做的就是将以下内容添加到〜/ .ssh / config文件中:

    ~ / .ssh / config

    Host bitbucket.org
      User TarekSaid
      Hostname bitbucket.org
      PreferredAuthentications publickey
      IdentitiesOnly yes
      IdentityFile ~/.ssh/bitbucket_rsa
    

    现在它正在运作 .

相关问题