Spring Integration SFTP:使用私人键

发布于 2025-02-14 01:11:28 字数 2197 浏览 1 评论 0原文

我有一些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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2025-02-21 01:11:29

解决方案是为会话工厂设置私钥:

DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(new JSch(), true);
byte[] privateKeyBytes = privateKey.getBytes(StandardCharsets.UTF_8);
factory.setPrivateKey(new ByteArrayResource(privateKeyBytes));
return setCommonConfigProperties(factory, sftpProperties, sftpUser);

The solution is to instead set the private key for the session factory:

DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(new JSch(), true);
byte[] privateKeyBytes = privateKey.getBytes(StandardCharsets.UTF_8);
factory.setPrivateKey(new ByteArrayResource(privateKeyBytes));
return setCommonConfigProperties(factory, sftpProperties, sftpUser);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文