Java JSchException: Auth cancel

旧时模样 提交于 2019-12-23 03:04:42

问题


I am currently seeing this issue when attempting to ssh into a box from using JSch. I have tested the connection using Cygwin and it connects seamlessly. I have generated the keypair and placed the Public key in authorized_keys file on the remote server.

Below is an extract from the logs

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

Code used to established connection

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

Here is the code for the 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;
}

回答1:


It looks like jsch isn't trying to use a key file, because your code doesn't tell jsch what key file to use. You need to call Jsch.addIdentity() to add one or more key files to the session:

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

or

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

There are other varieties of the addIdentity() function, if you want to supply the information in some other format.




回答2:


The "Auth cancel" is thrown when the authentication implementation throws JSchAuthCancelException. What in turn usually happens when the UserInfo implementation return false from one of its methods.

Your code does not show what is the ui. So I cannot provide more information until you show us more code.


Also you write about key pair, yet your code does not show any use of a key. You instead set a password.

For private key authentication with JSch see for example:
Can we use JSch for SSH key-based communication?



来源:https://stackoverflow.com/questions/30187755/java-jschexception-auth-cancel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!