Spring Cloud Config无法使用ssh密钥

时间:2016-10-10 15:45:50

标签: java spring bitbucket ssh-keys spring-cloud-config

我在Linux(arch)上,尝试使用ssh密钥在tutorial之后使用私有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

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

  

如果您不使用HTTPS和用户凭据,当您将密钥存储在默认目录(〜/ .ssh)中并且uri指向SSH位置时,SSH也应该开箱即用。 " git@github.com:配置/云配置&#34 ;.重要的是〜/ .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

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

1 个答案:

答案 0 :(得分:10)

终于成功了!

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

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

的〜/ .ssh /配置

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

现在它正在运作。