2016-04-28 8 views
2

httpsウェブサイトに接続するためにJSOUPを使用します。私はこの方法Jsoup.connect(URL)を使用しますが、それは例外をスロー:SSLをバイパスするGoogle Play

javax.net.ssl.SSLHandshakeException: Certificate not valid or trusted. 

だから私は、すべての証明書のSSL信頼するように、このコードを使用します。

public static void enableSSLSocket() throws KeyManagementException, NoSuchAlgorithmException { 
    TrustManager[] trustAllCerts = new TrustManager[]{ 
    new X509TrustManager() { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return null; 
     } 
     public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { } 
     public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { } 
    } 
}; 

// Install the all-trusting trust manager 
try { 
    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    HttpsURLConnection.setDefaultHostnameVerifier(
     new HostnameVerifier() { 
      public boolean verify(String urlHostName, SSLSession session) { 
       return true; 
      } 
     }); 
} 
catch (Exception e) { 
    //We can not recover from this exception. 
    e.printStackTrace(); 
}} 

をしかし、私は上記のコードを使用しSICEグール警告がありますプレイストアで。

あなたのアプリはApache HTTPクライアントでX509TrustManagerインターフェイスの安全でない実装を使用しており、セキュリティ上の脆弱性が生じます。この脆弱性の修正の締め切りを含む詳細については、このGoogleヘルプセンターの記事をご覧ください。

私はこのコードを追加した場合:

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
       try { 
        chain[0].checkValidity(); 
       } catch (Exception e) { 
        throw new CertificateException("Certificate not valid or trusted."); 
       } 
      } 

警告がなくなって、しかしJSOUPが動作していないが、同じ例外を除いて、再び機能していません。 すべてのSSLとバイパスのGoogleの警告を信頼する方法はありますか?事前にありがとう。

+3

"だから、すべてのSSLとバイパスのGoogleの警告を信頼する方法はありますか?" - うまくいけない。 "JSOUPは機能しません" - おそらく、あなたはJSoupの問題の[mcve]を提供し、 "うまくいかない"ということを詳細に説明する別のStack Overflow質問を開くことを検討するべきでしょう。 – CommonsWare

+0

@CommonWare私はちょうど私の質問を更新します。あなたが私を助けることを願っています! – hoanngothanh

+0

'enableSSLSocket()'メソッドを完全に削除することをお勧めします。 – CommonsWare

答えて

0

私はいくつかのいずれかをホップ、解決策を見つけるには、それを必要とする:

public static void trustSSLCertificate(final Activity mActivity, final DownloadPortalTask task){ 
 
     try { 
 
      HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 
 
       public boolean verify(String hostname, SSLSession session) { 
 
        return true; 
 
       } 
 
      }); 
 

 
      SSLContext context = SSLContext.getInstance("TLS"); 
 
      context.init(null, new X509TrustManager[]{new X509TrustManager() { 
 
       public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
 
       } 
 

 
       public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
 
        try { 
 
         chain[0].checkValidity(); 
 
        } catch (final Exception e) { 
 

 
         mActivity.runOnUiThread(new Runnable() { 
 
          @Override 
 
          public void run() { 
 
           AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); 
 
           AlertDialog alertDialog = builder.create(); 
 
           alertDialog.setCancelable(false); 
 
           String message = "There a problem with the security certificate for this web site."; 
 
           message += "\nDo you want to continue anyway?"; 
 
           alertDialog.setTitle("SSL Certificate Error"); 
 
           alertDialog.setMessage(message); 
 
           alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
 
            @Override 
 
            public void onClick(DialogInterface dialog, int which) { 
 
             acceptSSL = true; 
 
             return; 
 

 
            } 
 
           }); 
 

 
           alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
 
            @Override 
 
            public void onClick(DialogInterface dialog, int which) { 
 
             acceptSSL = true; 
 
             task.onInterruptedDownload(); 
 
            } 
 
           }); 
 
           alertDialog.show(); 
 

 
          } 
 

 
         }); 
 

 
         while(!acceptSSL){ 
 
          try{ 
 
           Thread.sleep(1000); 
 
          } catch(InterruptedException er) { } 
 
         } 
 

 
        } 
 
       } 
 
       public X509Certificate[] getAcceptedIssuers() { 
 
        return new X509Certificate[0]; 
 
       } 
 
      }}, new SecureRandom()); 
 
      HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory()); 
 
     } catch (Exception e) { // should never happen 
 
      e.printStackTrace(); 
 
     } 
 
    }

コールコールJsoup前にこの方法。 googleのSSL警告も消えてしまった。

関連する問題