获取"com.jcraft.jsch.JSchException:验证失败"–但"ssh"可以使用公钥身份验证登录 [英] Getting "com.jcraft.jsch.JSchException: Auth fail" – but "ssh" can login using public key authentication

查看:254
本文介绍了获取"com.jcraft.jsch.JSchException:验证失败"–但"ssh"可以使用公钥身份验证登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSch连接远程服务器,但是却出现验证失败" 异常.下面是我的代码:

I am trying to connect a remote server using JSch but I am getting "Auth fail" exception. Below is my code:

String user = "user.name";
String host = "hostip";
try
{
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host);
    session.setConfig("StrictHostKeyChecking", "no");
    System.out.println("Establishing Connection...");
    session.connect();
    int assinged_port=session.setPortForwardingL(lport, rhost, rport);
    System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
}
catch(Exception e){System.err.print(e);}

但是,当我尝试使用命令 ssh user.name@hostip 从iTerm进行SSH时,我可以使用公钥身份验证成功访问远程服务器.

However when I try to ssh from iTerm using the command ssh user.name@hostip I can successfully access the remote server using public key authentication.

推荐答案

您的OpenSSH ssh 命令自动使用您在 .ssh 文件夹中为OpenSSH配置的私钥.

Your OpenSSH ssh command automatically uses the private key you have configured for OpenSSH in your .ssh folder.

JSch不会自动使用OpenSSH密钥.您必须明确告诉它要使用什么密钥.
请参见我们可以使用JSch进行基于SSH密钥的通信吗?

JSch won't automatically use OpenSSH keys. You have to explicitly tell it what key to use.
See Can we use JSch for SSH key-based communication?

还请注意,JSch不支持OpenSSH所支持的所有密钥格式.
请参见使用JSch时

Also note that JSch does not support all key formats that OpenSSH do.
See "Invalid privatekey" when using JSch

强制性警告:请勿使用 StrictHostKeyChecking = no 盲目接受所有主机密钥.那是一个安全漏洞.您无法防御 MITM攻击.有关正确(安全)的方法,请参见:如何在使用JSch SFTP库时解析Java UnknownHostKey?

Obligatory warning: Do not use StrictHostKeyChecking=no to blindly accept all host keys. That is a security flaw. You lose a protection against MITM attacks. For the correct (and secure) approach, see: How to resolve Java UnknownHostKey, while using JSch SFTP library?

这篇关于获取"com.jcraft.jsch.JSchException:验证失败"–但"ssh"可以使用公钥身份验证登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆