android – 接受自签名SSL证书 – >在哪里设置默认的TrustManager

前端之家收集整理的这篇文章主要介绍了android – 接受自签名SSL证书 – >在哪里设置默认的TrustManager前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我不得不承认,我知道接受所有证书可以被视为没有安全性.我们有“真正的”Certs,但仅限于我们的现场系统.我们的测试系统上的证书是自签名的.因此,只要我们开发,我们必须使用测试服务器,这迫使我去禁用证书.

我在Stackoverflow上看到了很多toppics,而且整个网络都在尝试做同样的事情:
接受SSL证书.但是,这些答案似乎都不适用于我的问题,因为我没有弄乱HTTPSUrlConnections.

如果我正在请求代码通常看起来像这样(注释清除):

  1. //creates an HTTP-Post with an URL
  2. HttpPost post = createBaseHttpPost();
  3. //loads the request Data inside the httpPost
  4. post.setEntity(getHttpPostEntity());
  5. //appends some Headers like user-agend or Request UUIDs
  6. appendHeaders(post);
  7.  
  8. HttpClient client = new DefaultHttpClient();
  9. //mResponse is a custom Object which is returned
  10. //from the custom ResponseHandler(mResponseHandler)
  11. mResponse = client.execute(post,mResponseHandler);
  12. return mResponse;

我读到我应该注入自己的TrustManager和X509HostnameVerivier.我创建了这样的:

  1. private static final TrustManager[] TRUST_ALL_CERTS = new TrustManager[]{
  2. new X509TrustManager() {
  3.  
  4. public X509Certificate[] getAcceptedIssuers() {
  5. return new X509Certificate[]{};
  6. }
  7.  
  8. public void checkServerTrusted(X509Certificate[] chain,String authType)
  9. throws CertificateException {
  10. }
  11.  
  12. public void checkClientTrusted(X509Certificate[] chain,String authType)
  13. throws CertificateException {
  14. }
  15. }
  16.  
  17. };
  18.  
  19. private static X509HostnameVerifier ACCEPT_ALL_HOSTNAMES =
  20. new X509HostnameVerifier() {
  21.  
  22. public void verify(String host,String[] cns,String[] subjectAlts)
  23. throws SSLException {
  24. }
  25.  
  26. public void verify(String host,X509Certificate cert) throws SSLException {
  27. }
  28.  
  29. public void verify(String host,SSLSocket ssl) throws IOException {
  30. }
  31.  
  32. public boolean verify(String host,SSLSession session) {
  33. return true;
  34. }
  35. };

如果我在我的请求中注入HostnameVerifier像这样(客户端是上面的DefaultHttpClient)

  1. SSLSocketFactory ssl = (SSLSocketFactory)client.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
  2. ssl.setHostnameVerifier(ACCEPT_ALL_HOSTNAMES);

响应从“主机名**不匹配”变为“错误请求”.我想我必须设置TrustManager,但是我无法在我的请求中设置它,因为我没有使用HttpsUrlConnections在我看到它的任何地方.

解决方法

不,它不会强制您禁用验证,它会强制您正确实施验证.不要盲目接受所有证书.不,你的情况没有任何不同,你只需要信任Android默认不信任的证书.

您正在使用HttpClient,因此用于设置信任管理器的API与HttpsURLConnection有所不同,但过程是相同的:

>使用受信任证书(服务器的自签名证书)加载密钥库文件
>用它初始化KeyStore.
>使用2中的KeyStore创建一个SocketFactory.
>设置HTTP客户端库以在创建SSL套接字时使用它.

Android的文档中描述了这一点:http://developer.android.com/reference/org/apache/http/conn/ssl/SSLSocketFactory.html

有关该主题的更详细的文章显示了如何创建信任库文件http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

一些背景信息和示例代码http://nelenkov.blogspot.com/2011/12/using-custom-certificate-trust-store-on.html

这是初始化HttpClient所需的代码

  1. KeyStore localTrustStore = KeyStore.getInstance("BKS");
  2. InputStream in = getResources().openRawResource(R.raw.mytruststore);
  3. localTrustStore.load(in,TRUSTSTORE_PASSWORD.tocharArray());
  4.  
  5. SchemeRegistry schemeRegistry = new SchemeRegistry();
  6. schemeRegistry.register(new Scheme("http",PlainSocketFactory
  7. .getSocketFactory(),80));
  8. SSLSocketFactory sslSocketFactory = new SSLSocketFactory(localTrustStore);
  9. schemeRegistry.register(new Scheme("https",sslSocketFactory,443));
  10. HttpParams params = new BasicHttpParams();
  11. ClientConnectionManager cm =
  12. new ThreadSafeClientConnManager(params,schemeRegistry);
  13.  
  14. HttpClient client = new DefaultHttpClient(cm,params);

此时,您没有理由信任所有证书.如果你这样做,那一切都在你身上:)

猜你在找的Android相关文章