2016-09-08 9 views
0

javaを使用して2段階認証ログインを持つLinuxサーバーに接続しようとしています。私はこのスクリプトを実行すると、私は認証キーを入力していないと明らかに私は、「認証に失敗しました」エラーが出るJSCHライブラリを使用した2段階検証

session = jsch.getSession(username, ip); 
Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no"); 
session.setConfig(config); 

session.setPassword(password); 
session.connect(); 
System.out.println("Connected to " + ip); 

:私はJSCHライブラリを使用しています、これは私がこれまで持っているコードです。だから私はどのように確認キーを使用してログインするのですか?これが不可能な場合、誰かがこの機能を持つライブラリを提案する可能性があります。

これは、パテを使用してサーバーにログインしています。したがって、ユーザ名、時間ベースの生成コード、パスワードを入力します。 enter image description here

+0

あなたがいますコマンドラインsshユーティリティを使用してこのホストにログインできますか?このサーバにログインするにはsshを実行し、sshにデバッグ情報を出力させるには '-vvv'オプションを含めてください。次に、質問を編集して、実行したsshコマンドとそれが生成した出力を含めます。これは、人々がこの余分な検証がどのように行われているかを知るのに役立ちます。 – Kenster

+0

余分な検証は時間ベースであるため、実際には不可能です。そのため、秘密の文字列がjavaメソッドに渡され、検証の2番目の部分のキーが返されます。 –

+0

私の提案のポイントは、この検証ステップがsshプロトコルを通してどのように行われるかを理解することでした。 – Kenster

答えて

0

私はこれを最終的にうまくやったので、UserInfoとUIKeyboardInteractiveを実装する必要があります。それは私の作品次のコードに示す認証キーを、返却するために、promptKeyboardInteractiveメソッドを使用します。

import java.security.InvalidKeyException; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 
import com.jcraft.jsch.UIKeyboardInteractive; 
import com.jcraft.jsch.UserInfo; 

public class UserAuthKI{ 
    public static void main(String[] arg){ 

try{ 
    JSch jsch=new JSch(); 

    String host=""; 
    String user=""; 

    Session session=jsch.getSession(user, host, 22); 

    // username and passphrase will be given via UserInfo interface. 
    UserInfo ui=new MyUserInfo(); 
    session.setUserInfo(ui); 
    session.connect(); 

    Channel channel =session.openChannel("exec"); 
    ((ChannelExec)channel).setCommand("echo 'hello'"); 

    channel.setInputStream(System.in); 
    channel.setOutputStream(System.out); 

    channel.connect(); 

} 
catch(Exception e){ 
    System.out.println(e); 
} 
    } 

    public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 
public String getPassword(){ return "passwordHere"; } 
public boolean promptYesNo(String str){ 
    return true; 
} 

public String getPassphrase(){return null;} 
public boolean promptPassphrase(String message){ return false; } 
public boolean promptPassword(String message){ 
    return true; 
} 
public void showMessage(String message){ 
    System.out.println(message); 
} 

public String[] promptKeyboardInteractive(String destination, 
              String name, 
              String instruction, 
              String[] prompt, 
              boolean[] echo){ 

    System.out.println("destination: "+destination); 
    System.out.println("name: "+name); 
    System.out.println("instruction: "+instruction); 
    System.out.println("prompt.length: "+prompt.length); 

    String[] str = new String[1]; 

    if(prompt[0].contains("Password:")){ 
     str[0] = getPassword(); 
    } 
    else if(prompt[0].contains("Verification code: ")){ 
     try { 
      str[0] = PasswordUtils.verify_code("CODEHERE"); 
     } catch (InvalidKeyException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    else{ 
     str = null; 
    } 

    return str; 

    } 
    } 
} 

(PasswordUtils.verif_codeは()キーを生成し、静的メソッドである)

関連する問題