繁体   English   中英

使用私钥身份验证来保护FTP

[英]Secure FTP using private key authentication

我正在使用JSCH库在两个服务器之间执行sftp。 但是,我无法使用getResource()将文件加载到URL ,因此在运行时会出错。 我检查了权限,它们是600,所以我们应该可以阅读它。

    JSch jsch = new JSch();
    File file = new File("/home/xxxxxx/.ssh/id_rsa");
    Session session = null;
    URL keyFileURL = null;
    URI keyFileURI = null;
    if (file.exists())
    {
        keyFileURL = SecureFTP.class.getClassLoader().getResource("/home/xxxxxx/.ssh/id_rsa");
        if (keyFileURL == null)
        {
            System.out.println("what");
            throw new RuntimeException("Key file not found in classpath");
        }
    }
  try{
             keyFileURI = keyFileURL.toURI();
    }
    catch(Exception URISyntaxException)
    {
        System.out.println("Wrong URL. LOL");
}



    jsch.addIdentity(new File(keyFileURI).getAbsolutePath());

在加载资源或路径时我缺少什么吗?

为什么不使用File.toURL

File file = new File("/home/dev85adm/.ssh/id_rsa");
if (file.exists()) {
    URL keyFileURL = file.toURL();
    //...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM