title: Using SSH authentication with JGit to Access a Git Repository Securely / Auth fail
body: <p>I'm using JGit in scala to access a remote Git repo, and I need to use SSH for authentication. JGit uses JSch to provide secure access.</p>
  
  <p>I followed this tutorial : <a href="http://www.codeaffine.com/2014/12/09/jgit-authentication/" rel="noreferrer">http://www.codeaffine.com/2014/12/09/jgit-authentication/</a></p>
  
  <p>However, i have always, an  ,<strong>com.jcraft.jsch.JSchException: Auth fail</strong></p>
  
  <p>my method is:</p>
  
  <pre><code>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 =&gt; t
        case _ =&gt; throw new ClassCastException("Variable of type Transport cannot be cast to the SshTransport")
      }
      sshTransport.setSshSessionFactory(sshSessionFactory)
    }
  
  })
  </code></pre>
  
  <p>but I have always, this exception: </p>
  
  <pre class="lang-none prettyprint-override"><code>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.&lt;init&gt;(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
  </code></pre>