You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

JSch连接SFTP服务器时出现Authentication failure认证失败问题求助

JSch连接SFTP服务器时出现Authentication failure认证失败问题求助

各位大佬好,我现在用JSch(jcraft的SFTP工具库)连接远程SFTP服务器时,一直卡在会话连接环节,报Auth fail错误,但手动在终端用sftp user@host命令不用输密码就能成功连接,实在找不到问题所在,来求助大家!

错误栈信息:

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 com.evry.extraction.transfer.SFTPTransfer.connect(SFTPTransfer.java:62)
    at com.evry.extraction.transfer.SFTPTransfer.transferAll(SFTPTransfer.java:107)
    at com.evry.extraction.transfer.TransferHandler.run(TransferHandler.java:47)

出错的代码片段:

try {
    JSch ssh = new JSch();
    String privatekey = PropertiesUtil.getProperty(PropertiesUtil.Constant.SSH_PRIVATEKEY.getValue());
    this.LOG.info("Private key found: " + privatekey);
    ssh.addIdentity(privatekey);
    
    String username = PropertiesUtil.getProperty(PropertiesUtil.Constant.FTP_USERNAME.getValue());
    String host = PropertiesUtil.getProperty(PropertiesUtil.Constant.FTP_SERVER.getValue());
    this.LOG.info("Username: " + username);
    this.LOG.info("Host: " + host);
    
    session = ssh.getSession(username, host, 22);
    this.LOG.info("Got jcraft session");
    session.setConfig("StrictHostKeyChecking", "no");
    this.LOG.info("Set strictHostKeyChecking to -> no");
    
    session.connect(); // <- 就是这里失败
    this.LOG.info("Connected successfully");
    
    sftp = (ChannelSftp) session.openChannel("sftp");
    sftp.connect();
    this.LOG.info("sftp channel opened and connected");
    this.LOG.info("Exit connect");
} catch (JSchException e) {
    this.LOG.error("Connection failed: " + e.getMessage());
    this.LOG.info("SFTP connection failed: {}", e.toString(), e);
    // Re-throw as unchecked exception
    throw new RuntimeException("Failed to establish SFTP connection", e);
}

相关日志信息:

2025-06-06 16:40:09,458 [Thread-0] INFO tx.transfer.SFTPTransfer - Private key found: keys/id_rsa
2025-06-06 16:40:09,467 [Thread-0] INFO tx.transfer.SFTPTransfer - Username: xxxxx
2025-06-06 16:40:09,467 [Thread-0] INFO tx.transfer.SFTPTransfer - Host: 111.111.111.11
2025-06-06 16:40:09,472 [Thread-0] INFO tx.transfer.SFTPTransfer - Got jcraft session
2025-06-06 16:40:09,472 [Thread-0] INFO tx.transfer.SFTPTransfer - Set strictHostKeyChecking to -> no
2025-06-06 16:40:09,489 [Thread-0] INFO tx.logger.SftpLog - Connecting to 111.111.111.11 port 22
2025-06-06 16:40:09,497 [Thread-0] INFO tx.logger.SftpLog - Connection established
2025-06-06 16:40:09,516 [Thread-0] INFO tx.logger.SftpLog - Remote version string: SSH-2.0-OpenSSH_9.9_P2
2025-06-06 16:40:09,517 [Thread-0] INFO tx.logger.SftpLog - Local version string: SSH-2.0-JSCH-0.1.54
2025-06-06 16:40:09,517 [Thread-0] INFO tx.logger.SftpLog - CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
2025-06-06 16:40:09,542 [Thread-0] INFO tx.logger.SftpLog - CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
2025-06-06 16:40:09,647 [Thread-0] INFO tx.logger.SftpLog - CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
2025-06-06 16:40:09,650 [Thread-0] INFO tx.logger.SftpLog - SSH_MSG_KEXINIT sent
2025-06-06 16:40:09,650 [Thread-0] INFO tx.logger.SftpLog - SSH_MSG_KEXINIT received
2025-06-06 16:40:09,650 [Thread-0] INFO tx.logger.SftpLog - kex: server: sntrup761x25519-sha512,sntrup761x25519-sha512@openssh.com,mlkem768x25519-sha256,curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-s,kex-strict-s-v00@openssh.com
2025-06-06 16:40:09,650 [Thread-0] INFO tx.logger.SftpLog - kex: server: rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
2025-06-06 16:40:09,651 [Thread-0] INFO tx.logger.SftpLog - kex: server: aes128-ctr,aes192-ctr,aes256-ctr,chacha20-poly1305@openssh.com,aes128-gcm@openssh.com,aes256-gcm@openssh.com
2025-06-06 16:40:09,651 [Thread-0] INFO tx.logger.SftpLog - kex: server: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
2025-06-06 16:40:09,651 [Thread-0] INFO tx.logger.SftpLog - kex: server: none,zlib@openssh.com
2025-06-06 16:40:09,651 [Thread-0] INFO tx.logger.SftpLog - kex: server: 
2025-06-06 16:40:09,651 [Thread-0] INFO tx.logger.SftpLog - kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
2025-06-06 16:40:09,651 [Thread-0] INFO tx.logger.SftpLog - kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521

我已经排查的点:

  • 确认代码里加载的私钥路径正确,日志已打印Private key found: keys/id_rsa,该路径的私钥和手动连接使用的是同一个
  • 手动执行sftp user@host可直接连接,无需输入密码,说明服务器已信任本地公钥
  • 代码中已设置StrictHostKeyChecking=no,排除主机密钥检查的影响
  • 注意到服务器SSH版本是OpenSSH_9.9_P2,本地JSch版本为0.1.54,怀疑是否存在版本兼容性问题

有没有大佬遇到过类似情况?求指点可能的原因和排查方向!

内容来源于stack exchange

火山引擎 最新活动