首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SSH身份验证结合JGit访问Git安全/8月失败

使用SSH身份验证结合JGit访问Git安全/8月失败
EN

Stack Overflow用户
提问于 2015-10-29 09:16:35
回答 3查看 10.8K关注 0票数 6

我在scala中使用JGit访问远程Git,我需要使用SSH进行身份验证。JGit使用JSch提供安全访问。

我遵循了本教程:http://www.codeaffine.com/2014/12/09/jgit-authentication/

然而,我总是,一个,com.jcraft.jsch.JSchException:,Auth失败的

我的方法是:

代码语言:javascript
复制
def sshCloneRemoteRepository() = {

// 1 / Override SSH configuration with JschConfigSessionFactory
val sshSessionFactory: SshSessionFactory  = new JschConfigSessionFactory() {

  override protected def createDefaultJSch(fs: FS): JSch = {
    val defaultJSch = super.createDefaultJSch(fs)
    defaultJSch.removeAllIdentity()
    defaultJSch.addIdentity(new File("/home/xw/.ssh/id_rsa").getAbsolutePath)
    defaultJSch.setKnownHosts("/home/xw/.ssh/known_hosts")
    val configRepository:ConfigRepository = com.jcraft.jsch.OpenSSHConfig.parseFile("/home/xw/.ssh/config")
    defaultJSch.setConfigRepository(configRepository)
    defaultJSch
  }

  override protected def configure(host:OpenSshConfig.Host, session:Session ) {
    // This still checks existing host keys and will disable "unsafe" authentication mechanisms
    // if the hostkey doesn't match.
    session.setConfig("StrictHostKeyChecking", "no")
  }
}

// 2 / Preparing cloneCommand
val cloneCommand: CloneCommand = Git.cloneRepository()
cloneCommand.setURI(sshOriginRepositoryPath)
cloneCommand.setDirectory(localRepository)
cloneCommand.setRemote(originBranch)
cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
  override def configure(transport:Transport ) {
    val sshTransport:SshTransport = transport match {
      case t:SshTransport => t
      case _ => throw new ClassCastException("Variable of type Transport cannot be cast to the SshTransport")
    }
    sshTransport.setSshSessionFactory(sshSessionFactory)
  }

})

但我一直有这样的例外:

代码语言:javascript
复制
org.eclipse.jgit.api.errors.TransportException: ssh://git@vod.canal-bis.com:22/edt/json-edito.git: Auth fail
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:139) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:193) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:133) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at fr.canal.face.services.TGitServicee$class.sshCloneRemoteRepository(GitServicee.scala:109) ~[classes/:na]
    at fr.canal.face.services.GitServicee$.sshCloneRemoteRepository(GitServicee.scala:22) [classes/:na]
    at fr.canal.face.biz.actors.EditoPullerActor.cloneOrPull(EditoPullerActor.scala:50) [classes/:na]
    at fr.canal.face.biz.actors.EditoPullerActor$$anonfun$receive$1.applyOrElse(EditoPullerActor.scala:34) [classes/:na]
    at akka.actor.Actor$class.aroundReceive(Actor.scala:465) [akka-actor_2.11-2.3.6.jar:na]
    at fr.canal.face.biz.actors.EditoPullerActor.aroundReceive(EditoPullerActor.scala:22) [classes/:na]
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) [akka-actor_2.11-2.3.6.jar:na]
    at akka.actor.ActorCell.invoke(ActorCell.scala:487) [akka-actor_2.11-2.3.6.jar:na]
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238) [akka-actor_2.11-2.3.6.jar:na]
    at akka.dispatch.Mailbox.run(Mailbox.scala:220) [akka-actor_2.11-2.3.6.jar:na]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393) [akka-actor_2.11-2.3.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.2.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.2.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.2.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.2.jar:na]
Caused by: org.eclipse.jgit.errors.TransportException: ssh://git@vod.canal-bis.com:22/edt/json-edito.git: Auth fail
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:159) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:136) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:262) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:161) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1138) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:130) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    ... 17 common frames omitted
Caused by: com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:512) ~[jsch-0.1.50.jar:na]
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116) ~[org.eclipse.jgit-3.7.0.201502260915-r.jar:3.7.0.201502260915-r]
    ... 24 common frames omitted
EN

回答 3

Stack Overflow用户

发布于 2015-10-30 12:17:27

我解决了我的问题,代码很好,但是Auth 问题是由我在..ssh/config文件(我的机器=> Amazon => Stach Git Server )中的代理配置引起的,所以本教程中的代码:http://www.codeaffine.com/2014/12/09/jgit-authentication/ word很好,谢谢Rudiger

票数 4
EN

Stack Overflow用户

发布于 2020-09-21 08:42:24

尝试生成一个新的密钥使用ssh-keygen -t rsa -m PEM作为默认键

票数 2
EN

Stack Overflow用户

发布于 2018-08-31 06:09:40

你也可以这么做

代码语言:javascript
复制
override protected def createDefaultJSch(fs: FS): JSch = {
....
session.setConfig("StrictHostKeyChecking", "no") //instead of configure()
....
defaultJSch
}

而不是配置函数,这应该可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33410092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档