Spring Integration 5.5升级6.2.3后SFTP保活配置及警告问题
解决Spring Integration 6.2.3 SFTP保活配置与ProFTPD保活警告问题
一、正确配置SFTP保活(替代原JSch的ServerAlive设置)
Spring Integration 6.x切换到Apache SSHD后,原JSch的setServerAliveInterval/setServerAliveCountMax方法被替换为SSHD原生的保活配置,通过SshClient直接设置对应属性即可:
private SshClient customSshClient() { final SshClient client = SshClient.setUpDefaultClient(); // 对应原serverAliveInterval:每隔指定毫秒发送保活包 client.setKeepAliveInterval(keepAliveInterval); // 对应原serverAliveCountMax:保活失败超过该次数则断开会话 client.setKeepAliveCountMax(keepAliveCountMax); // 保留原有其他配置 List<RequestHandler<ConnectionService>> oldHandlers = client.getGlobalRequestHandlers(); List<RequestHandler<ConnectionService>> handlers = new ArrayList<>(); if (GenericUtils.isNotEmpty(oldHandlers)) { handlers.addAll(oldHandlers); } handlers.add(KeepAliveHandler.INSTANCE); client.setGlobalRequestHandlers(handlers); client.addPasswordIdentity(password); client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE); return client; }
二、消除keepalive@proftpd.org警告
该警告源于ProFTPD服务器主动发送的自定义通道请求,Apache SSHD默认不识别该请求导致输出日志。由于官方修复尚未发布,可通过以下方式解决:
方案1:自定义通道请求处理器
创建处理器拦截并处理该自定义请求,避免警告输出:
public class ProFtpdKeepAliveRequestHandler implements ChannelRequestHandler { public static final ProFtpdKeepAliveRequestHandler INSTANCE = new ProFtpdKeepAliveRequestHandler(); private static final String PROFTPD_KEEPALIVE_REQUEST = "keepalive@proftpd.org"; @Override public Result handle(Channel channel, String requestType, Buffer buffer, boolean wantReply) throws IOException { if (PROFTPD_KEEPALIVE_REQUEST.equals(requestType)) { // 响应服务器的保活请求(若需要回复) if (wantReply) { channel.sendSuccess(); } return Result.ReplySent; } // 其他请求交给默认处理器 return Result.Unknown; } }
修改自定义SessionFactory,将处理器注册到SFTP通道:
public class CustomSftpSessionFactory extends DefaultSftpSessionFactory { public CustomSftpSessionFactory(final SshClient sshClient, final boolean isSharedSession) { super(sshClient, isSharedSession); } @Override protected SftpClient createSftpClient(ClientSession clientSession, SftpVersionSelector initialVersionSelector, SftpErrorDataHandler errorDataHandler) throws IOException { SftpClient client = super.createSftpClient(clientSession, initialVersionSelector, errorDataHandler); // 获取SFTP通道并添加自定义处理器 DefaultSftpClient.DefaultSftpChannelSubsystem sftpChannel = (DefaultSftpClient.DefaultSftpChannelSubsystem) client.getChannel(); sftpChannel.addChannelRequestHandler(ProFtpdKeepAliveRequestHandler.INSTANCE); return client; } }
方案2:精准调整日志级别(备用)
若自定义处理器无效,可直接针对输出警告的类调整日志级别:
在application.properties中配置:
logging.level.org.springframework.integration.sftp.session.DefaultSftpClient$SftpChannelSubsystem=ERROR
或在logback.xml中配置:
<logger name="org.springframework.integration.sftp.session.DefaultSftpClient$SftpChannelSubsystem" level="ERROR"/>
总结
- 通过
SshClient的setKeepAliveInterval/setKeepAliveCountMax完成保活配置,完全替代原JSch的功能; - 自定义
ChannelRequestHandler处理ProFTPD的自定义保活请求,从根源消除警告; - 若处理器方案无效,可通过精准调整日志级别隐藏警告。
内容的提问来源于stack exchange,提问作者Kamil
相关产品推荐
相关产品推荐

