簡體   English   中英

JSch PAM 身份驗證 - “身份驗證失敗” - 憑據正確

[英]JSch PAM authentication - “Auth fail” - credentials are correct

我正在構建一個 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();

如果您在服務器端使用 PAM 身份驗證,則可能需要在客戶端實現鍵盤交互身份驗證。

請參閱“密碼”和“鍵盤交互”有什么區別? 問題來了解 PAM 和鍵盤交互式身份驗證之間的關系。


鍵盤交互認證實現示例,參見官方UserAuthKI示例

基本上,您需要實現UIKeyboardInteractive接口(與UserInfo接口一起)並使用Session.setUserInfo將實現與會話相關聯。

如果身份驗證僅提示輸入單個“密碼”,請實現UIKeyboardInteractive.promptKeyboardInteractive方法以返回帶有密碼的單個元素數組。


強制性警告:不要使用StrictHostKeyChecking=no盲目接受所有主機密鑰。 這是一個安全漏洞。 你失去了對MITM 攻擊的保護。 有關正確(且安全)的方法,請參閱:如何在使用 JSch SFTP 庫時解析 Java UnknownHostKey?

下面的代碼已經過測試並且按我的預期工作。 試試下面的代碼:

 /**
     * 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM