Spring Integration 6.1.0中如何为DefaultSftpSessionFactory设置密码优先认证
解决Spring Integration 6.1.0设置SFTP首选密码认证的问题
在Spring Integration 6.x版本中,DefaultSftpSessionFactory的setSessionConfig()方法已被移除,取而代之的是直接通过setClientConfigurer()方法配置底层的SshClient实例,以此设置首选认证方式。以下是两种可行的配置方案:
方案一:直接设置SshClient的首选认证方式
@Bean SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost("someSFTPServer"); factory.setUser("user"); factory.setPassword("SomePassword"); factory.setPort(22); factory.setAllowUnknownKeys(true); // 配置客户端优先使用密码认证 factory.setClientConfigurer(sshClient -> { sshClient.setPreferredAuthentications("password"); }); return new CachingSessionFactory<>(factory); }
方案二:通过Properties配置认证参数
如果需要复用原有Properties配置逻辑,可以通过SshClient的setProperties()方法传入配置:
@Bean Properties sftpConfigProperties() { Properties config = new Properties(); config.setProperty("PreferredAuthentications", "password"); return config; } @Bean SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory(Properties sftpConfigProperties) { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost("someSFTPServer"); factory.setUser("user"); factory.setPassword("SomePassword"); factory.setPort(22); factory.setAllowUnknownKeys(true); factory.setClientConfigurer(sshClient -> { sshClient.setProperties(sftpConfigProperties); }); return new CachingSessionFactory<>(factory); }
原理说明
Spring Integration 6.0重构了SFTP客户端实现,底层切换为Apache SSHD的最新API,原有的SessionConfig相关方法被替换为直接操作SshClient的配置接口。设置PreferredAuthentications为password后,客户端会优先尝试密码认证,避免因长时间等待keyboard-interactive认证导致的连接超时问题,与Filezilla/Cyberduck的认证逻辑保持一致。
内容的提问来源于stack exchange,提问作者user14724924
相关产品推荐
相关产品推荐

