JSch登录Sftp失败

young 562 2021-11-03

问题

使用JSch登录sftp登录失败。

原因

sftp设置了Kerberos 身份验证,登录时会有检验提示,线程卡在校验中无法向下进行。

解决方法:

添加 config.put("PreferredAuthentications", "publickey,keyboard-interactive,password"); 跳过校验即可

原代码

JSch jsch = new JSch();
Session session = jsch.getSession(uploadParameter.getUsername(), uploadParameter.getUrl(),
 uploadParameter.getPort());
session.setPassword(uploadParameter.getPassword());
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(uploadParameter.getSftpSoTimeout());
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect(uploadParameter.getSftpSoTimeout());

在原代码的基础上添加跳过Kerberos校验

JSch jsch = new JSch();
 Session session = jsch.getSession(uploadParameter.getUsername(), uploadParameter.getUrl(),
 uploadParameter.getPort());
 session.setPassword(uploadParameter.getPassword());
 Properties config = new Properties();
 config.put("StrictHostKeyChecking", "no");
//跳过Kerberos校验
config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
 session.setConfig(config);
 session.setTimeout(uploadParameter.getSftpSoTimeout());
 session.connect();
 ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
 channel.connect(uploadParameter.getSftpSoTimeout());