我在 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
// …Run Code Online (Sandbox Code Playgroud)