Java JSchException : Auth cancel

标签 java ssh jsch

我目前在尝试使用 JSch 通过 ssh 进入盒子时遇到此问题。我已经使用 Cygwin 测试了连接,它可以无缝连接。我已生成 key 对并将公钥放置在远程服务器上的 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 没有尝试使用 key 文件,因为您的代码没有告诉 jsch 要使用哪个 key 文件。您需要调用 Jsch.addIdentity() 将一个或多个 key 文件添加到 session 中:

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

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

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

关于Java JSchException : Auth cancel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187755/

相关文章:

java - "Auth fail"在 jsch-0.1.42 与 Java 1.4.2

ant - 如何在 ant 构建文件中指定 JAR 的路径?

java - 如何设计带有任何媒体文件预览的 android 聊天气泡

java - 等距图 block 选择

bash - bash 中嵌套命令的 Heredoc

ssh - Amazon EC2 上的权限被拒绝(公钥)

java - 如何让选号器不可取消?

java - 如何在 Spring 请求映射中接受多个可选参数,但一次只能接受一个?

git - 从自定义git repo构建

scala - 从 Spark 服务器执行 SFTP 时,大型机服务器上的记录级别数据截断