Spring Integration SFTP:使用私人键
我有一些Hickups为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
I had a few hickups setting up private keys for Spring Integration SFTP.
Thought I may share my findings here.
I read elsewhere that I should parameterize the JSch object with the private key.
This, however, is not working:
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;
}
Resulting Exception:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是为会话工厂设置私钥:
The solution is to instead set the private key for the session factory: