【发布时间】: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
-
有点离题,但仍然很重要:永远不要将任何密码字符串硬编码到您的生产二进制文件中。
-
那你要使用密码还是公钥认证?
-
我要做的就是公钥认证