我目前在尝试使用 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/