Hou*_*ama 7 git ssh scala jsch jgit
我在 scala 中使用 JGit 访问远程 Git 存储库,并且需要使用 SSH 进行身份验证。JGit 使用 JSch 提供安全访问。
我遵循了本教程:http://www.codeaffine.com/2014/12/09/jgit-authentication/
但是,我总是出现com.jcraft.jsch.JSchException:身份验证失败
我的方法是:
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)
}
})
Run Code Online (Sandbox Code Playgroud)
但我一直有这个例外:
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)
}
})
Run Code Online (Sandbox Code Playgroud)
我解决了我的问题,代码很好,但是身份验证失败问题是由我的.ssh/config 文件中的代理配置引起的(我的机器 => Amazon AWS Machine => Stach Git Server),所以代码在本教程: http://www.codeaffine.com/2014/12/09/jgit-authentication/ 字很好,谢谢Rudiger