2017-03-23 32 views
1

リモートディレクトリのファイルのリストを作成するSFTPクラスを構築しています。私はそうするためにJSchライブラリを使用しています。私はすでにユーザーを設定しており、手動でリモートサーバーにSSHを設定することはできます。 JSCHが接続しようとしたときしかし、それはJSch PAM認証 - "認証失敗" - 資格情報が正しい

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(); 
+0

あなたはコマンドラインからユーザー名/パスワードを使用していますか? PAMの認証は通常サーバー側ですので、それは問題ではないと思います。 – Gray

+0

'ssh -v'の出力を表示します(私の答えが役に立たない場合)。 –

+0

+サイドノート: 'StrictHostKeyChecking = no'を使わないでください - あなたは[中間者攻撃](https://en.wikipedia.org/wiki/Man-in-the-middle_attack)に対する保護を失いつつあります。 。 –

答えて

2

あなたはサーバー側にPAM認証を使用している場合、それはあなたがクライアント側のキーボードインタラクティブ認証を実装する必要がある可能性が高いです。

PAMとキーボードインタラクティブ認証の関係を理解するには、What's the difference between “password” and “keyboard-interactive”?の質問を参照してください。


キーボード対話型認証の実装例については、official UserAuthKI exampleを参照してください。

基本的にUIKeyboardInteractive interfaceUserInfo interfaceと一緒に)を実装し、Session.setUserInfoを使用して実装にセッションを関連付ける必要があります。

認証で単一の「パスワード」のみが要求されている場合は、UIKeyboardInteractive.promptKeyboardInteractive methodを実装して、パスワード付きの単一エレメント配列を返します。

+1

マーティン、あなたは男です。完璧に働いた。 –

1

以下のコードはテストされており、期待通りに動作します。ただ、コードの下に試してみてください。

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

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

+0

は、 'transferProtocol'に' null '値を渡します。 –

+0

-1 - この回答はどのようにしてOPに役立ちますか?あなたのコードは質問と同じです+結果を説明せずに誰も 'StrictHostKeyChecking = no'を使用しないように勧めましょう! –

関連する問題