Public Key Authentication

In Working with the Client API we briefly covered how to authenticate the client. To authenticate we place a set of Authenticator implementations on the SshContext. The client iterates over these attempting each authentication in turn until the server notifies it that the connection has been authenticated.

The first task is to tell the client the remote username for this connection.

    sshContext.setUsername("lee");
		

The following Authenticator implementations are available for Public Key authentication

Private Key File

This Authenticator takes a java.io.File and a passphrase.

    sshContext.addAuthenticator(
            new PrivateKeyFileAuthenticator(new File(".ssh/id_rsa"), "xxxxx"));

 

Key Pairs

This Authenticator takes a variable number of pre-loaded SshKeyPair objects. This requires that you load the key pair in a separate operation and can be used where the key pair is not necessarily stored in a file.

    sshContext.addAuthenticator(new PublicKeyAuthenticator(
	  SshPrivateKeyFileFactory.parse(
	      new FileInputStream(".ssh/id_rsa")).toKeyPair("xxxxx")));

 You may add as many key pairs as you need. The Authenticator will verify which key is suitable for authentication before attempting the actual authentication.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.