@Override protected JSch createDefaultJSch(FS fs) throws JSchException { JSch jSch = super.createDefaultJSch(fs); // apply customized private key if (privateKeyPath != null) { jSch.removeAllIdentity(); jSch.addIdentity(privateKeyPath.toString()); } return jSch; } };
class CustomConfigSessionFactory extends JschConfigSessionFactory { @Override protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException { JSch jsch = super.getJSch(hc, fs); jsch.removeAllIdentity(); jsch.addIdentity( "/path/to/private/key" ); return jsch; } }
private void clearIdentityRepository() throws JSchException { jsch.setIdentityRepository( null ); // revert to default identity repo jsch.removeAllIdentity(); }
private void clearIdentityRepository() throws JSchException { jsch.setIdentityRepository( null ); // revert to default identity repo jsch.removeAllIdentity(); }
SshSessionFactory factory = new JschConfigSessionFactory() { public void configure(Host hc, Session session) { session.setConfig("StrictHostKeyChecking", "no"); } @Override protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException { JSch jsch = super.getJSch(hc, fs); jsch.removeAllIdentity(); //Where getSshKey returns content of the private key file if (StringUtils.isNotEmpty(data.getSshKey())) { jsch.addIdentity("identityName", data.getSshKey() .getBytes(), null, data.getSshPassphrase() .getBytes()); } return jsch; } };
jsch.removeAllIdentity(); session = jsch.getSession(user, host, port); session.setPassword(password);
private boolean setAgent(JSch jsch, String identityFile, boolean preferAgent) throws JSchException { boolean agentUsed = false; if (preferAgent) { Connector con = ConnectorFactory.getInstance().createConnector(ConnectorFactory.ConnectorKind.ANY); if (con != null) { IdentityRepository irepo = new IdentityRepositoryImpl(con); jsch.setIdentityRepository(irepo); agentUsed = true; } } if (!agentUsed) { jsch.setIdentityRepository(null); // remove all identity files jsch.removeAllIdentity(); // and add the one specified by CredentialsProvider if (StringUtils.hasText(identityFile)) { jsch.addIdentity(identityFile); } } return agentUsed; }