The following code works to connect via SFTP, the directory path of the key is used in order to load it.
Here's the current code ( I've specified the libraries just in case )
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
SSHClient sftp = new SSHClient();
KeyProvider privateKey = sftp.loadKeys("/home/sample/.ssh/id_rsa");
sftp.addHostKeyVerifier(new PromiscuousVerifier());
sftp.connect("111.222.333.444");
sftp.authPublickey("sample", privateKey);
Yes, you can invoke the follwoing method of the library to support the use-case.
/**
* Creates a {@link KeyProvider} instance from passed strings. Currently only PKCS8 format private key files are
* supported (OpenSSH uses this format).
* <p/>
*
* @param privateKey the private key as a string
* @param publicKey the public key as a string if it's not included with the private key
* @param passwordFinder the {@link PasswordFinder} that can supply the passphrase for decryption (may be {@code
* null} in case keyfile is not encrypted)
*
* @return the key provider ready for use in authentication
*
* @throws SSHException if there was no suitable key provider available for the file format; typically because
* BouncyCastle is not in the classpath
* @throws IOException if the key file format is not known, etc.
*/
public KeyProvider loadKeys(String privateKey, String publicKey, PasswordFinder passwordFinder) throws IOException
If that is not available in the version of library you're using then considered upgrading it.
<dependency>
<groupId>net.schmizz</groupId>
<artifactId>sshj</artifactId>
<version>0.10.0</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.