Praveen Billampati
Praveen Billampati

Reputation: 73

DefaultSftpSessionFactory using privateKey string

I am looking for a way to create DefaultSftpSessionFactory using private key string. The different functions available in this are using the private Key Resource(local file) instead.

Any ideas to create SessionFactory needed for SftpRemoteFileTemplate? I have the user, host and the private key as a String.

-Thanks

Upvotes: 2

Views: 3049

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

The setter takes a Resource...

/**
 * Allows you to set a {@link Resource}, which represents the location of the
 * private key used for authenticating against the remote host. If the privateKey
 * is not provided, then the {@link DefaultSftpSessionFactory#setPassword(String) password}
 * property is mandatory (or {@link #setUserInfo(UserInfo) userInfo} that returns a
 * password.
 * @param privateKey The private key.
 * @see JSch#addIdentity(String)
 * @see JSch#addIdentity(String, String)
 */
public void setPrivateKey(Resource privateKey) {
    this.privateKey = privateKey;
}

There are many kinds of Resource, including ByteArrayResource, where you can use

setPrivateKey(new ByteArrayResource(myKeyString.getBytes());

Upvotes: 2

Related Questions