问题
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