Apache Commons and JSch both require a private key file to set up an SFTP connection. The project I'm working on will be used to connect to multiple SFTP servers. Therefore, we do not hope to deploy multiple private key files, but rather keep these keys as strings in an encrypted config file. Is there an SFTP library that doesn't require a file object for private key?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The JSch has an addIdentity
method overload that takes the key from a buffer:
public class JSch {
...
public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase) throws JSchException{
Alternatives:
There is also an addIdentity
overload that takes an Identity
interface:
public class JSch {
...
public void addIdentity(Identity identity, byte[] passphrase)
Just implement the interface to get the private key from wherever you need.
See IdentityFile
for an example implementation.
Alternatively, store all your keys to IdentityRepository
.
public interface IdentityRepository {
...
public boolean add(byte[] identity);
public class JSch {
...
public synchronized IdentityRepository getIdentityRepository()