Spring Integration SFTP新版本升级问题:密钥验证及连接异常
SSH连接问题排查与解决
错误日志
[sshd-SshClient[thread-4] WARN o.a.s.c.k.AcceptAllServerKeyVerifier - Server at server presented unverified RSA key: SHA256:key
[sshd-SshClient[thread-3] WARN o.a.s.c.s.ClientSessionImpl - exceptionCaught(ClientSessionImpl[server])[state=Opened] IOException: Connection reset
[sshd-SshClient[thread-1] INFO o.a.s.c.s.ClientSessionImpl - Disconnecting(ClientSessionImpl[server]): SSH2_DISCONNECT_PROTOCOL_ERROR - Detected IdleTimeout after 600609/600000 ms.
1. 服务器RSA密钥未验证问题
此前依赖已知主机文件完成密钥验证,因Spring Integration团队对apache-sshd的实现方式变更,导致原有验证逻辑失效。
解决方案
手动配置SSH客户端的服务器密钥验证器,指定使用本地已知主机文件:
Java配置方式
@Bean public SshClientFactory sshClientFactory() { DefaultSshClientFactory factory = new DefaultSshClientFactory(); // 指定已知主机文件路径,可根据实际路径调整 File knownHostsFile = new File(System.getProperty("user.home") + "/.ssh/known_hosts"); factory.setServerKeyVerifier(new KnownHostsServerKeyVerifier(knownHostsFile)); return factory; }
XML配置方式
<int-ssh:client-factory id="sshClientFactory"> <int-ssh:server-key-verifier> <bean class="org.apache.sshd.client.keyverifier.KnownHostsServerKeyVerifier"> <constructor-arg value="#{systemProperties['user.home']}/.ssh/known_hosts"/> </bean> </int-ssh:server-key-verifier> </int-ssh:client-factory>
2. 连接重置与空闲超时问题(心跳配置)
通过配置SSH心跳机制可解决此类问题,在Spring Integration中可通过自定义SshClient实现:
解决方案
Java配置方式
@Bean public SshClient customSshClient() { return SshClient.setUpDefaultClient() // 忽略心跳失败,避免因单次心跳问题断开连接 .setKeepAliveProvider(KeepAliveProvider.of(KeepAliveProvider.HeartbeatOption.IGNORE_FAILURE)) // 设置心跳间隔为30秒,可根据实际需求调整 .setHeartbeatInterval(Duration.ofSeconds(30)) .build(); } @Bean public SshClientFactory sshClientFactory() { return new DefaultSshClientFactory(() -> customSshClient()); }
XML配置方式
<!-- 自定义SshClient,配置心跳参数 --> <bean id="customSshClient" class="org.apache.sshd.client.SshClient" factory-method="setUpDefaultClient"> <property name="keepAliveProvider"> <bean class="org.apache.sshd.common.keepalive.KeepAliveProvider" factory-method="of"> <constructor-arg value="IGNORE_FAILURE"/> </bean> </property> <!-- 心跳间隔30秒,格式为ISO-8601 --> <property name="heartbeatInterval" value="PT30S"/> </bean> <!-- 配置Spring Integration SSH客户端工厂,使用自定义SshClient --> <int-ssh:client-factory id="sshClientFactory" ssh-client-ref="customSshClient"/>
内容的提问来源于stack exchange,提问作者jagadeesh
相关产品推荐
相关产品推荐

