2015-05-12 36 views
0

我目前看到这个问题,当试图SSH入使用JSch框。我已经使用Cygwin测试了连接,并且它可以无缝连接。我已经生成密钥对并将公钥放在远程服务器上的authorized_keys文件中。Java JSchException:身份验证取消

下面是从日志中提取用于建立连接

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); 

这里

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 

代码是用户信息界面

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; 
} 
+0

您可以编辑您的问题,包括您使用调用jsch,使这个连接的代码?如果您使用密钥文件,密钥文件的名称是什么? – Kenster

+0

对不起,我对SSH相当陌生我相信我使用的密钥是id_rsa – joey7492

+0

有点偏离主题,但仍然很重要:永远不要将任何密码字符串硬编码到生产二进制文件中。 – gustafbstrom

回答

1

它看起来像jsch ISN代码试图使用密钥文件,因为你的代码不会告诉jsch使用什么密钥文件。你需要调用到一个或多个关键文件添加到会话:

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

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

还有其他品种addIdentity()的功能,如果你想在其他一些供应信息格式。

+0

我已经添加了这个代码,它指向相应的键,但我仍然有相同的问题 – joey7492

1

当身份验证实现抛出JSchAuthCancelException时,将引发“身份验证取消”。当UserInfo实现从其一个方法返回false时通常会发生什么。

您的代码不显示什么是ui。所以在向我们展示更多代码之前,我无法提供更多信息。


你也写密钥对,但是你的代码并不显示任何使用钥匙。您改为设置密码。

对于JSch私钥认证,参见例如:
Can we use JSch for SSH key-based communication?

+0

我编辑了代码,我还添加了addIdentity() ' – joey7492

+0

所以向我们展示你的代码并用'addIdentity()'记录下来。 –