【发布时间】:2022-07-08 23:32:58
【问题描述】:
我在为 Spring Integration SFTP 设置私钥时遇到了一些小问题。
我想我可以在这里分享我的发现。
我在别处读到我应该用私钥参数化 JSch 对象。 但是,这不起作用:
private SessionFactory<ChannelSftp.LsEntry> createSftpSessionFactoryForPrivateKeyCredentials(
SftpProperties sftpProperties, String sftpUser, String privateKey) throws JSchException {
byte[] privateKeyBytes = privateKey.getBytes(StandardCharsets.UTF_8);
JSch javaSecureChannel = javaSecureChannelForPrivateKey(privateKeyBytes);
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(javaSecureChannel, true);
return setCommonConfigProperties(factory, sftpProperties, sftpUser);
}
private JSch javaSecureChannelForPrivateKey(byte[] privateKey) throws JSchException {
JSch javSecureChannel = new JSch();
javSecureChannel.addIdentity("key", privateKey, /* public key */
null, /* private key password */ null);
return javSecureChannel;
}
产生的异常:
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:404)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:61)
at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:85)
at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:82)
at org.springframework.integration.util.SimplePool.doGetItem(SimplePool.java:200)
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:181)
... 32 more
Caused by: java.lang.IllegalArgumentException: either a password or a private key is required
at org.springframework.util.Assert.isTrue(Assert.java:121)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.initJschSession(DefaultSftpSessionFactory.java:418)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:393)
... 37 more
【问题讨论】:
标签: java spring-integration sftp private-key spring-integration-sftp