错误日志
[sshd-SshClient[thread-4] 警告 o.a.s.c.k.AcceptAllServerKeyVerifier - 服务器上的服务器提供了未经验证的 RSA 密钥:SHA256:key
[sshd-SshClient[thread-3] 警告 o.a.s.c.s.ClientSessionImpl - 异常捕获(ClientSessionImpl[服务器])[状态=已打开] IOException:连接>重置 [sshd-SshClient[thread-1] 信息 o.a.s.c.s.ClientSessionImpl - 断开连接(ClientSessionImpl[服务器]):SSH2_DISCONNECT_PROTOCOL_ERROR - 600609/600000 毫秒后检测到 IdleTimeout。
对于第一行日志,已知的主机文件之前已被接受,但现在由于 spring 集成团队的 apache-sshd 实现而无法接受,而对于第二行,当我检查时发现我们可以设置 heatbeat 但在 spring 集成中不知道如何设置通过
@配置 公共类 SftpConfiguration {
@Value("${sftp.known-hosts}")
private Resource knownHosts;
@Value("${sftp.host}")
private String host;
@Value("${sftp.port}")
private int port;
@Value("${sftp.username}")
private String user;
@Value("${sftp.privatekey}")
private Resource privateKey;
@Value("${sftp.privateKeyPassphrase}")
private String privateKeyPassphrase;
@Bean(name = "sftpSessionFactory")
public DefaultSftpSessionFactory sftpSessionFactory() throws IOException {
SshClient externalClient = SshClient.setUpDefaultClient();
// serverKeyverifier will help to set the knowHosts
ResourceKnownHostsServerKeyVerifier serverKeyVerifier = null;
if (this.knownHosts != null) {
serverKeyVerifier = new ResourceKnownHostsServerKeyVerifier(knownHosts);
}
externalClient.setServerKeyVerifier(serverKeyVerifier);
if (this.privateKey != null) {
IoResource<Resource> privateKeyResource = new AbstractIoResource<>(Resource.class, this.privateKey) {
public InputStream openInputStream() throws IOException {
return (this.getResourceValue()).getInputStream();
}
};
try {
Collection<KeyPair> keys = SecurityUtils.getKeyPairResourceParser().
loadKeyPairs(null, privateKeyResource,FilePasswordProvider.of(this.privateKeyPassphrase));
externalClient.setKeyIdentityProvider(KeyIdentityProvider.wrapKeyPairs(keys));
} catch (GeneralSecurityException ex) {
throw new IOException("Cannot load private key: " + this.privateKey.getFilename(), ex);
}
}
//以下属性用于心跳 CoreModuleProperties.HEARTBEAT_INTERVAL.set(externalClient, Duration.ofSeconds(10));
DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(externalClient, false);
sftpSessionFactory.setHost(host);
sftpSessionFactory.setPort(port);
sftpSessionFactory.setUser(user);
return sftpSessionFactory;
}
}