我在scala中使用JGit来访问远程Git仓库,我需要使用SSH进行身份验证。 JGit使用JSch提供安全访问。
我遵循了本教程:http://www.codeaffine.com/2014/12/09/jgit-authentication/
但是,我一直都是, com.jcraft.jsch.JSchException:Auth fail
我的方法是:
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)
}
})
但我一直都是这个例外:
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
答案 0 :(得分:2)
我解决了我的问题,代码很好,但验证失败问题是由我的 .ssh / config文件中的代理配置引起的(< em>我的机器=&gt;亚马逊AWS机器=&gt; Stach Git服务器),所以本教程中的代码: http://www.codeaffine.com/2014/12/09/jgit-authentication/ 好的,谢谢Rudiger
答案 1 :(得分:0)
您也可以这样做
override protected def createDefaultJSch(fs: FS): JSch = {
....
session.setConfig("StrictHostKeyChecking", "no") //instead of configure()
....
defaultJSch
}
代替配置功能,这应该可以工作。
答案 2 :(得分:0)
try生成了一个新的密钥,使用ssh-keygen -t rsa -m PEM
作为默认密钥