【问题标题】:Java JSchException: Auth cancelJava JSchException:身份验证取消
【发布时间】:2015-05-13 16:13:11
【问题描述】:

我目前在尝试通过使用 JSch 来 ssh 进入一个盒子时看到了这个问题。我已经使用 Cygwin 测试了连接,它可以无缝连接。我已生成密钥对并将公钥放在远程服务器上的authorized_keys 文件中。

以下是日志的摘录

INFO: Next authentication method: publickey
INFO: Authentications that can continue: keyboard-interactive,password
INFO: Next authentication method: keyboard-interactive
INFO: Authentications that can continue: password
INFO: Next authentication method: password
INFO: Disconnecting from xx.xx.xx.xx port 22
com.jcraft.jsch.JSchException: Auth cancel

用于建立连接的代码

Properties config = new Properties();
config.put("cipher",",aes256-cbc");
config.put("mac.c2s", "hmac-sha2-256");
config.put("KEXs", "diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256");
config.put("StrictHostKeyChecking", "no");
Session session = jsch.getSession(username,host,port);
session.setPassword(password);
session.setUserInfo(ui);
session.setConfig(config);
session.getPort();
session.connect();
session.setPortForwardingL(tunnelLocalPort,tunnelRemoteHost,tunnelRemotePort);

这是 UserInfo ui 的代码

String password = null;

@Override
public String getPassphrase() {
    return null;
}
@Override
public String getPassword() {
    return password;
}
public void setPassword(String passwd) {
    password = passwd;
}

@Override
public boolean promptPassphrase(String message) {
    return false;
}
@Override
public boolean promptPassword(String message) {
    return false;
}
@Override
public boolean promptYesNo(String message) {
    return false;
}

【问题讨论】:

  • 您能否编辑您的问题以包含您用来调用 jsch 以建立此连接的代码?如果您使用的是密钥文件,那么密钥文件的名称是什么?
  • 对不起,我对 SSH 还很陌生,我相信我使用的密钥是 id_rsa
  • 有点离题,但仍然很重要:永远不要将任何密码字符串硬编码到您的生产二进制文件中。
  • 那你要使用密码还是公钥认证?
  • 我要做的就是公钥认证

标签: java ssh jsch


【解决方案1】:

看起来 jsch 没有尝试使用密钥文件,因为您的代码没有告诉 jsch 要使用哪个密钥文件。您需要调用Jsch.addIdentity() 将一个或多个密钥文件添加到会话中:

jsch.addIdentity("C:\users\jdoe\.ssh\id_rsa");

String passphrase = ...;
jsch.addIdentity("C:\users\jdoe\.ssh\id_rsa",
                 passphrase.getBytes());

如果您想以其他格式提供信息,addIdentity() 函数还有其他变体。

【讨论】:

  • 我已将此添加到代码中,它指向相应的键,但我仍然遇到同样的问题
【解决方案2】:

当身份验证实现抛出JSchAuthCancelException 时,会抛出“Auth cancel”。当 UserInfo 实现从其方法之一返回 false 时,通常会发生什么。

您的代码没有显示ui 是什么。因此,在您向我们展示更多代码之前,我无法提供更多信息。


您还写了关于密钥对的文章,但您的代码没有显示任何密钥的使用。您改为设置密码。

有关使用 JSch 的私钥身份验证,请参阅例如:
Can we use JSch for SSH key-based communication?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多