Java SSH 使用 JSch 与 key 连接 - 身份验证失败

标签 java authentication ssh jsch ssh-keys

我正在尝试使用带 key 的 SSH 连接到本地主机。但是我仍然得到一个错误:

Auth Failed

方法实现如下:

public void downloadUsingPublicKey(String username, String host)
{
     String privateKey = "~/.ssh/id_rsa";
     
     JSch jsch = new JSch(); 
     Session session = null; 
     Channel channel = null; 
     ChannelSftp channelSftp = null; 
     try 
     { 
         jsch.addIdentity(privateKey); 
         System.out.println("Private Key Added."); 
         
         session = jsch.getSession(username, host); 
         System.out.println("session created."); 
         
         java.util.Properties config = new java.util.Properties(); 
         config.put("StrictHostKeyChecking", "no"); 
         
         session.setConfig(config); 
         session.connect(); 
         
         channel = session.openChannel("sftp"); 
         channel.connect(); System.out.println("shell channel connected...."); 
         
         channelSftp = (ChannelSftp)channel; 
         channelSftp.cd(Config.dir); 
         
         System.out.println("Changed the directory..."); 
     } catch (JSchException e) 
     { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
     } catch (SftpException e) 
     { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
     }finally
     { 
         if(channelSftp!=null)
         { 
             channelSftp.disconnect(); 
             channelSftp.exit(); 
         } 
         if(channel!=null) channel.disconnect(); 
         if(session!=null) session.disconnect(); 
     } 
}

我使用 Linux 终端创建了我的公钥/私钥对,如下所示:

ssh-keygen -t rsa -b 4096 -C "myemail@email.com"

我没有放任何短语。

下一步:

ssh-add ~/.ssh/id_rsa

最后

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

然后当我运行我的程序时出现错误:

com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:512)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at pl.eroj.filedownloader.Downloader.downloadUsingPublicKey(Downloader.java:73)
    at pl.eroj.filedownloader.Downloader.main(Downloader.java:107)

有什么想法吗?我的 key 是 OpenSSH 类型,以 line 开头 “-----开始 RSA 私钥-----”

最佳答案

这就是我使用私钥路径连接本地的方式 当
privateKey Path ="C:\Keys\private key.ppk"

    public static OutputStream ConnectionUsingKey(String user, String hostName, String privateKeyPath)
        throws JSchException, IOException {
    JSch jsch = new JSch();
    Session session = null;
    try {

        jsch.addIdentity(privateKeyPath);
        session = jsch.getSession(user, hostName, 22);
        session.setConfig("PreferredAuthentications", "publickey");
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        if (session.isConnected() == true) {
            System.out.println("Connection to Session server is successfully");
        }
        channel = session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect(30 * 1000);
        return channel.getOutputStream();

    } catch (JSchException e) {
        throw new RuntimeException("Failed to create Jsch Session object.", e);
    }
}

关于Java SSH 使用 JSch 与 key 连接 - 身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31703743/

相关文章:

java - 尝试从另一个子类继承时出错?

java - 将 int 值赋给字符串

node.js - 如果用户未经过身份验证,则显示公共(public)逻辑 React JS 和 Node API

php - zend framework 2 认证服务

mysql - 使用 ssh 导入 .sql 文件

git - 在 Dockerfile 中使用 git clone "ssh://"

ssh - 如何通过使用 ssh-keygen 生成新 key 对在两个 AMI EC2 实例之间建立 SSH 连接

java - 为什么以及在哪里调用 setLocale

java - 在给定时间找到所有人活着的快速算法?

c# - 如何获取用户的事件目录 token ?