Java Code Examples for com.jcraft.jsch.session#setIdentityRepository()

The following examples show how to use com.jcraft.jsch.session#setIdentityRepository() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AgentProxyAwareJschConfigSessionFactory.java    From gitflow-incremental-builder with MIT License 6 votes vote down vote up
@Override
protected Session createSession(Host hc, String user, String host, int port, FS fs)
        throws JSchException {
    JSch jSch = getJSch(hc, fs);

    // assumption: identities from agent are always unencrypted
    final Collection<Identity> allUnencryptedIdentities = getIdentitiesFromAgentProxy();

    @SuppressWarnings("unchecked")
    Collection<Identity> identities = ((Collection<Identity>) jSch.getIdentityRepository().getIdentities());
    identities.stream()
            .filter(id -> !id.isEncrypted())
            .forEach(allUnencryptedIdentities::add);
    
    Session session = jSch.getSession(user, host, port);
    session.setIdentityRepository(new ReadOnlyIdentityRepository(allUnencryptedIdentities));
    return session;
}
 
Example 2
Source File: SFtpUtil.java    From Aria with Apache License 2.0 5 votes vote down vote up
/**
 * 创建jsch 的session
 *
 * @param threadId 线程id,默认0
 * @throws JSchException
 * @throws UnsupportedEncodingException
 */
public Session getSession(FtpUrlEntity entity, int threadId) throws JSchException,
    UnsupportedEncodingException {

  JSch jSch = new JSch();

  IdEntity idEntity = entity.idEntity;

  if (idEntity.prvKey != null) {
    if (idEntity.pubKey == null) {
      jSch.addIdentity(idEntity.prvKey,
          entity.password == null ? null : idEntity.prvPass.getBytes("UTF-8"));
    } else {
      jSch.addIdentity(idEntity.prvKey, idEntity.pubKey,
          entity.password == null ? null : idEntity.prvPass.getBytes("UTF-8"));
    }
  }

  setKnowHost(jSch, entity);

  Session session;
  if (TextUtils.isEmpty(entity.user)) {
    session = jSch.getSession(null, entity.hostName, Integer.parseInt(entity.port));
  } else {
    session = jSch.getSession(entity.user, entity.hostName, Integer.parseInt(entity.port));
  }
  if (!TextUtils.isEmpty(entity.password)) {
    session.setPassword(entity.password);
  }
  Properties config = new Properties();

  // 不检查公钥,需要在connect之前配置,但是不安全,no 模式会自动将配对信息写入know_host文件
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config);// 为Session对象设置properties
  session.setTimeout(5000);// 设置超时
  session.setIdentityRepository(jSch.getIdentityRepository());
  session.connect();
  SFtpSessionManager.getInstance().addSession(session, threadId);
  return session;
}