【问题标题】:JSch PAM authentication - "Auth fail" - credentials are correctJSch PAM 身份验证 - “身份验证失败” - 凭据正确
【发布时间】:2017-03-23 18:06:26
【问题描述】:

我正在构建一个 SFTP 类,负责列出远程目录的文件。我正在使用 JSch 库来执行此操作。我已经设置了一个用户,我可以手动 SSH 到远程服务器就好了。但是,当 JSch 尝试连接时,它会响应

com.jcraft.jsch.JSchException:身份验证失败

我注意到了一件事;当我手动 SSH 到服务器时,我看到它正在使用“PAM 身份验证”。我错过了什么?

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
Session session = jSch.getSession(username, destination, port);
session.setPassword(password);
session.setConfig(config);
session.connect();

【问题讨论】:

    标签: java ssh sftp jsch pam


    【解决方案1】:

    如果您在服务器端使用 PAM 身份验证,您可能需要在客户端实现键盘交互身份验证。

    请参阅What's the difference between “password” and “keyboard-interactive”? 问题以了解 PAM 和键盘交互式身份验证之间的关系。


    对于键盘交互认证实现示例,请参见official UserAuthKI example

    基本上你需要实现UIKeyboardInteractive interface(连同UserInfo interface)并使用Session.setUserInfo将实现与会话相关联。

    如果身份验证仅提示输入单个“密码”,请实现 UIKeyboardInteractive.promptKeyboardInteractive method 以返回带有密码的单个元素数组。


    强制警告:不要使用StrictHostKeyChecking=no 盲目接受所有主机密钥。那是一个安全漏洞。您失去了对MITM attacks 的保护。有关正确(且安全)的方法,请参阅:How to resolve Java UnknownHostKey, while using JSch SFTP library?

    【讨论】:

      【解决方案2】:

      下面的代码已经过测试并按我的预期工作。试试下面的代码:

       /**
           * Transfer a file to remote destination via JSCH library using sFTP protocol
           * 
           * @param username String remote SFTP server user name.
           * @param password String remote SFTP server user password
           * @param host String remote SFTP server IP address or host name.
           * @param file File to transfer to SFTP Server.
           * @param transferProtocol protocol to transfer a file. {@link FileTransferProtocol}
           * @return boolean true if file is transfered otherwise false.
           * @throws ApplicationException
           */
          public static boolean transferFile(final String username, final String password, final String host,
                                             final File file, final FileTransferProtocol transferProtocol) {
        //      throws ApplicationException {
              // currently can deal with sftp only.
       //       LOGGER.trace("Invoking transferFile...");
              JSch jsch = new JSch();
              try {
                  Session session = jsch.getSession(username, host);
       //           LOGGER.debug("Session Host: " + session.getHost());
                  session.setPassword(password);
                  Properties properties = new Properties();
                  properties.put("StrictHostKeyChecking", "no");
                  session.setConfig(properties);
       //           LOGGER.debug("Connecting to a session Host Server...");
                  session.connect();
       //           LOGGER.debug("session is established with host server.");
       //           Channel channel = session.openChannel(transferProtocol.ftpStringRepresentation());
                  Channel channel = session.openChannel("sftp");
       //           LOGGER.debug("Connecting to a sftp Channel...");
                  channel.connect();
       //           LOGGER.debug("Connected with sftp Channel.");
                  ChannelSftp channelSftp = (ChannelSftp) channel;
                  channelSftp.put(new FileInputStream(file), file.getName());
       //           LOGGER.debug("File transfered successfully");
                  channelSftp.exit();
       //           LOGGER.debug("sftp channel disconnected.");
                  channel.disconnect();
       //           LOGGER.debug("channel disconnected.");
                  session.disconnect();
        //          LOGGER.debug("session disconnected.");
                  return true;
              } catch (JSchException | FileNotFoundException | SftpException e) {
                  e.printStackTrace();
      //            LOGGER.error(e.getMessage(), e.getCause());
      //            throw new ApplicationException(e.getMessage(), ApplicationSeverity.ERROR, e.getCause(), e);
              }
              return false;
          }
      

      网址:https://github.com/SanjayMadnani/com.sanjay.common.common-utils/blob/master/common-utils/src/main/java/com/sanjay/common/util/FileUtil.java

      【讨论】:

      • transferProtocol 传递null 值。
      • -1 - 这个答案如何帮助 OP?您的代码与问题相同 + 永远不要在不解释后果的情况下建议任何人使用 StrictHostKeyChecking=no
      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 2016-05-10
      • 2018-07-12
      • 2022-01-10
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多