2017-11-21 17 views
0

Jsoupを使用してウェブサイトにログインするには何が必要ですか?私は自分のコードが正しいと信じていますが、Jsoupを使ってウェブサイトにログインしたことがないので、何か不足している可能性があります。ここに私のコードです:。ウェブサイトにログインする(Jsoup)

try {            
     String url = ("http://quadrigacx.com/login"); 


     Connection.Response loginForm = (Connection.Response)Jsoup.connect(url) 
        .method(Connection.Method.GET) 
        .execute(); 





     Document loginDoc = loginForm.parse(); 


     Elements loginElements = loginDoc.select("input:not([name=client_id]):not([name=password])"); 

     int i = 0; 

     String v[] = new String[loginElements.size()]; 

     for (Element element: loginElements){ 
      v[i++] = element.attr("value"); 
     } 


     int ii = 0; 

     String n[] = new String[loginElements.size()]; 

     for (Element element: loginElements){ 
      n[ii++] = element.attr("name"); 
     } 


     Connection.Response loginFormLogin = (Connection.Response)Jsoup.connect(url) 
       .cookies(loginForm.cookies())   
       .data(n[0],v[0]) 
       .data("client_id", "xxxxxxx") 
       .data("password", "xxxxxx") 
       .data(n[1],v[1]) 
       .data(n[2],v[2]) 
       .method(Connection.Method.POST) 
       .execute(); 


     Document document2 = Jsoup.connect("http://quadrigacx.com/settings") 
      .cookies(loginFormLogin.cookies()) 
      .get(); 
     System.out.print(document2.toString()); 
    } catch (IOException ex) { 
     Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } 

document2は、それが正常にログインしていない示してログインページを返すありtimeという名前の入力値であり、私はそれが動作しない理由、それはあるかもしれないと思います。時間の経過とともに上昇する値です。私はコードを2回実行し、時間変数は15112268601511226876を返しました。私のコードは文書を印刷するのに約10秒かかるので、timeの変数は投稿要求を送信するまでに既に変更されている可能性がありますか?これが問題かどうかはわかりません。多分私は見ていない何か他のものがありますか?ありがとう。

編集:ここにコードがあります。私はすでにユーザーIDとパスワードでログインした後に認証を送信します。 loginCookiesは最初のログインのCookieです。 Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", googleCode.getText()) .data("email_code", emailCode.getText()) .data("authenticate", "Authenticate") .followRedirects(true) .execute(); 私も試してみました: byte[] gcText = googleCode.getText().getBytes(ISO_8859_1); String gcValue = new String(gcText, UTF_8); byte[] ecText = emailCode.getText().getBytes(ISO_8859_1); String ecValue = new String(ecText, UTF_8); Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", gcValue) .data("email_code", ecValue) .data("authenticate", "Authenticate") .followRedirects(true) .execute();

+0

パスワードがサーバーに「そのまま」送信されていません。ブラウザの開発ツールを開くと、何らかの方法でハッシュ/暗号化されていることがわかります。そのため、プロセスを把握して模倣する必要があります。私はSHA256とQCXという名前のスクリプトから始めます。別のオプションは、サイトにAPIがあるかどうかを確認することです。 – TDG

+0

呼び出されたSHA256が見つかりませんでしたが、この '$(document).ready(function(){ if(clientId && pword) $( '#password')。val(QCX.security.hashクライアントのIDがハッシュされていることを意味しますか? –

+0

@TDGこのような場合、私は手動でログインできた方が簡単だろうと思います –

答えて

1

をあなたのリクエストにより多くのパラメータを追加する必要があります。 CSRF、時間、空のテーブルとハッシュ

コード:

import org.jsoup.Connection; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Element; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.nio.charset.StandardCharsets; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.Scanner; 

public class Quadrigacx { 
    public static String[] TABLE = new String[]{}; // Add data here 

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException { 

     final String URL = "https://www.quadrigacx.com/login/"; 
     String password = "password"; 
     String clientId = "id"; 

     String hashPassword = getHash(new String[]{clientId, password}); 

     Connection.Response response = Jsoup.connect(URL) 
       .userAgent("Mozilla") 
       .method(Connection.Method.GET) 
       .execute(); 

     Element csrf = response.parse().select("input[name=csrf]").first(); 
     Element time = response.parse().select("input[name=time]").first(); 
     Element hash = response.parse().select("input[name=hash]").first(); 

     Jsoup.connect(URL) 
       .userAgent("Mozilla") 
       .method(Connection.Method.POST) 
       .cookies(response.cookies()) 
       .data("password", hashPassword) 
       .data("client_id", clientId) 
       .data("csrf", csrf.attr("value")) 
       .data("time", time.attr("value")) 
       .data("hash", hash.attr("value")) 
       .execute(); 

     String googleCode = ""; 

     while (!googleCode.matches("^(?=[0-9]+)\\d{6}$")) { 
      System.out.print("Please enter the Two-Factor Authentication to validate your login. (Numbers Only): "); 
      Scanner in = new Scanner(System.in); 
      googleCode = in.nextLine(); 
     } 

     Jsoup.connect("https://www.quadrigacx.com/authenticate") 
       .userAgent("Mozilla") 
       .method(Connection.Method.POST) 
       .cookies(response.cookies()) 
       .data("google_code", googleCode) 
       .data("redirect", "dash") 
       .data("authenticate", "Authenticate") 
       .execute(); 

     response = Jsoup.connect("https://www.quadrigacx.com/dash/") 
       .userAgent("Mozilla") 
       .cookies(response.cookies()) 
       .method(Connection.Method.GET) 
       .execute(); 

     System.out.println(response.body()); 

    } 

    private static String getHash(String[] loginArray) throws NoSuchAlgorithmException, UnsupportedEncodingException { 
     StringBuilder h = new StringBuilder(); 
     for (String data : loginArray) 
      h.append(data).append(getSalt(data)); 

     MessageDigest digest = MessageDigest.getInstance("SHA-256"); 
     byte[] byteHash = digest.digest(h.toString().getBytes(StandardCharsets.UTF_8)); 

     StringBuilder sb = new StringBuilder(byteHash.length * 2); 
     for (byte b : byteHash) 
      sb.append(String.format("%02x", b)); 

     return sb.toString(); 
    } 

    private static String getSalt(String arg) throws UnsupportedEncodingException { 
     return TABLE[getLastByte(arg)]; 
    } 

    private static int getLastByte(String login) throws UnsupportedEncodingException { 
     final byte[] utf8Bytes = login.getBytes("UTF-8"); 
     return utf8Bytes[utf8Bytes.length - 1]; 
    } 
} 

ここに見つけることができる完全な作業コード(非常に長い):

https://pastebin.com/aBf1M3fX

+0

コードがうまくいきましたが、今は "Two-Factor Authentication"サービスを使ってログインしようとしています。私はいくつかの掘り下げをして、これを見つけた '

'これは私がg utf 8にoogleと電子メールコード?私は自分のコードを更新できるように自分のコードを更新します。 –

+0

@sandraburgleコードを編集しました。 – Sestertius

+0

助けてくれてありがとう、私は '!(?= [0-9] +)\\ d {6} $") 'を意味しますか? emailCodeを取得するために同じメソッドを使用できる場合に備えて、より良い理解をしたいと思います。ありがとう! –

関連する問題