I am able to connect to he SFTP host using Apache VFS2 library but when I try to connect via Jsch library using username and password getting Auth fail error.
I am not sure is it issue with password which has special characters or any other issue.
But I feel there is some difference in the Authentication process of Jsch and Apache VFS2. I mostly focusing on the password authentication.
I tired with setting PreferredAuthentications as password and also using the UserInfo object also still facing the same issue. it tries to attempt authentication for 6 times and then disconnecting with exception.
public class TestConnection {
public static void main(String[] args) throws UnsupportedEncodingException {
String host = "host";
String username = "username";
String password = "passwordWith%!#";
int port = 22;
// Enable JSch logging
JSch.setLogger(new Logger() {
public boolean isEnabled(int level) {
// Enable all log levels
return true;
}
public void log(int level, String message) {
// Print log messages to standard output
System.out.println("JSch Log: " + message);
}
});
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
System.out.println("password: " + password);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("kex",
"diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521");
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "password");
session.setConfig(config);
session.connect();
if (session.isConnected()) {
System.out.println("Connected to " + host);
// Open SFTP channel
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.disconnect();
}
session.disconnect();
System.out.println("Completed !!");
} catch (JSchException e) {
e.printStackTrace();
}
}
}
I am getting below logs.
JSch debug: SSH_MSG_NEWKEYS sent
JSch debug: SSH_MSG_NEWKEYS received
JSch debug: SSH_MSG_SERVICE_REQUEST sent
JSch debug: SSH_MSG_SERVICE_ACCEPT received
JSch debug: Authentications that can continue: password
JSch debug: Next authentication method: password
JSch debug: Disconnecting from sftp.testuser.com port 22
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:519)
at com.jcraft.jsch.Session.connect(Session.java:183)
at TestConnection.main(TestConnection.java:45)