diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java index f92c8f49f4..f2dbbebc50 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java @@ -25,6 +25,7 @@ import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.file.remote.session.SharedSessionCapable; import org.springframework.util.Assert; +import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; import com.jcraft.jsch.ChannelSftp.LsEntry; @@ -42,6 +43,7 @@ import com.jcraft.jsch.UserInfo; * @author Oleg Zhurakousky * @author Gunnar Hillert * @author Gary Russell + * @author David Liu * * @since 2.0 */ @@ -367,12 +369,12 @@ public class DefaultSftpSessionFactory implements SessionFactory, Share // private key if (this.privateKey != null) { - String privateKeyFilePath = this.privateKey.getFile().getAbsolutePath(); + byte[] keyByteArray = StreamUtils.copyToByteArray(this.privateKey.getInputStream()); if (StringUtils.hasText(this.privateKeyPassphrase)) { - this.jsch.addIdentity(privateKeyFilePath, this.privateKeyPassphrase); + this.jsch.addIdentity(this.user, keyByteArray, null, this.privateKeyPassphrase.getBytes()); } else { - this.jsch.addIdentity(privateKeyFilePath); + this.jsch.addIdentity(this.user, keyByteArray, null, null); } } com.jcraft.jsch.Session jschSession = this.jsch.getSession(this.user, this.host, this.port); diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpServerTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpServerTests.java index 3fe26d00af..910e4c2faf 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpServerTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpServerTests.java @@ -30,7 +30,6 @@ import java.security.PublicKey; import java.security.spec.RSAPublicKeySpec; import java.util.Arrays; -import com.jcraft.jsch.ChannelSftp.LsEntry; import org.apache.sshd.SshServer; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.file.FileSystemView; @@ -45,13 +44,17 @@ import org.apache.sshd.server.session.ServerSession; import org.apache.sshd.server.sftp.SftpSubsystem; import org.junit.Test; +import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.test.util.SocketUtils; import org.springframework.util.StreamUtils; +import com.jcraft.jsch.ChannelSftp.LsEntry; + /** * @author Gary Russell + * @author David Liu * @since 4.1 * */ @@ -150,7 +153,8 @@ public class SftpServerTests { f.setHost("localhost"); f.setPort(port); f.setUser("user"); - f.setPrivateKey(new ClassPathResource("id_rsa")); + InputStream stream = new ClassPathResource("id_rsa").getInputStream(); + f.setPrivateKey(new ByteArrayResource(StreamUtils.copyToByteArray(stream))); Session session = f.getSession(); doTest(server, session); }