发表日期:2016-11 文章编辑:小灯 浏览次数:2179
简单讲,https 是在http的基础上增加了SSL/TLS协议。
详细参见HTTPS传输加密原理
1)受信证书(由安卓认可的证书颁发机构, 或这些机构的下属机构颁发的证书)详细参见受信任的证书颁发机构
2)不受信证书(没有得到安卓认可的证书颁发机构颁发的证书)
3)自签名证书(自己颁发的证书, 分临时性的(在开发阶段使用)或在发布的产品中永久性使用的两种)
1)免费( 购买受信任机构颁发的证书每年要交 100 到 500 美元不等的费用. 自签名证书不花一分钱)
2)普及率高(自签名证书在手机应用中的普及率较高 ,跟用电脑浏览网页不同, 手机的应用一般就固定连一台服务器)
3)方便(在开发阶段写的代码,测试跟发布的时候也可以用)
1)受信证书,不需要修改代码,直接使用,就像SSL/TLS协议透明
2)不受信证书和自签名证书,需要修改Volley库代码(Volley底层支持,但是没有暴露出来方法)
1)clone volley库
a.从Google Repositoryclone
b.从清华镜像clone
2)代码修改
import android.content.Context; import android.util.Log;import java.io.IOException; import java.io.InputStream; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory;import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; import java.security.cert.X509Certificate; public class SSLSocketHelper { private static TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) { final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0]; return new TrustManager[]{ new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return originalTrustManager.getAcceptedIssuers(); }public void checkClientTrusted(X509Certificate[] certs, String authType) { try { if (certs != null && certs.length > 0){ certs[0].checkValidity(); } else { originalTrustManager.checkClientTrusted(certs, authType); } } catch (CertificateException e) { Log.w("checkClientTrusted", e.toString()); } }public void checkServerTrusted(X509Certificate[] certs, String authType) { try { if (certs != null && certs.length > 0){ certs[0].checkValidity(); } else { originalTrustManager.checkServerTrusted(certs, authType); } } catch (CertificateException e) { Log.w("checkServerTrusted", e.toString()); } } } }; }public static SSLSocketFactory getSSLSocketFactoryByCertificate(Context context,String keyStoreType, int keystoreResId) throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream caInput = context.getResources().openRawResource(keystoreResId);Certificate ca = cf.generateCertificate(caInput); caInput.close();if (keyStoreType == null || keyStoreType.length() == 0) { keyStoreType = KeyStore.getDefaultType(); } KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca);String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore);TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, wrappedTrustManagers, null);return sslContext.getSocketFactory(); }public static SSLSocketFactory getSSLSocketFactoryByKeyStore(Context context,String keyStoreType, int keystoreResId, String keyPassword) throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {InputStream caInput = context.getResources().openRawResource(keystoreResId);// creating a KeyStore containing trusted CAsif (keyStoreType == null || keyStoreType.length() == 0) { keyStoreType = KeyStore.getDefaultType(); } KeyStore keyStore = KeyStore.getInstance(keyStoreType);keyStore.load(caInput, keyPassword.toCharArray());// creating a TrustManager that trusts the CAs in the KeyStoreString tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore);TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, wrappedTrustManagers, null);return sslContext.getSocketFactory(); } }
4)详细可参看GitHub工程
GitHub工程与原始的volley对比:
a.增加了cache包,com.android.volley.ssl包,com.android.volley.utils包
b.在com.android.volley.toolbox包中,新增ByteRequest.java,GsonRequest.java,JsonArrayPostRequest.java,JsonObjectPostRequest.java,修改了Volley.java
c.只有com.android.volley.ssl包和Volley.java与支持https自签名证书有关系
一般是运维搞,可参考如下链接:
使用 OpenSSL 生成自签名证书
基于OpenSSL自建CA和颁发SSL证书
使用openssl生成自签名证书以及nginx ssl双向验证
创建并部署自签名的 SSL 证书到 Nginx
import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.SslErrorHandler; import android.net.http.SslError;private WebView webView;webView = (WebView) findViewById(R.id.my_webview); webView.setWebViewClient(new WebViewClient() {@Override public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); } });
国内镜像加速Android源码下载
通过 HTTPS 和 SSL 确保安全
Certificate authority
清华大学开源软件镜像站
Does the Web View on Android support SSL?
Android _实现SSL解决不受信任的证书问题
Using Android Volley With Self-Signed SSL Certificate
Android volley self signed HTTPS trust anchor for certification path not found
Android 网络--我是怎么做的: Volley+OkHttp+Https
Making a HTTPS request using Android Volley
1)上层使用(HttpService.java 修改)
2)最上层使用(BaseActivity.java修改)
日期:2018-04 浏览次数:6761
日期:2017-02 浏览次数:3435
日期:2017-09 浏览次数:3656
日期:2017-12 浏览次数:3526
日期:2018-12 浏览次数:4816
日期:2016-12 浏览次数:4582
日期:2017-07 浏览次数:13642
日期:2017-12 浏览次数:3505
日期:2018-06 浏览次数:4265
日期:2018-05 浏览次数:4444
日期:2017-12 浏览次数:3556
日期:2017-06 浏览次数:3979
日期:2018-01 浏览次数:3941
日期:2016-12 浏览次数:3908
日期:2018-08 浏览次数:4423
日期:2017-12 浏览次数:3706
日期:2016-09 浏览次数:6404
日期:2018-07 浏览次数:3206
日期:2016-12 浏览次数:3230
日期:2018-10 浏览次数:3378
日期:2018-10 浏览次数:3479
日期:2018-09 浏览次数:3577
日期:2018-02 浏览次数:3595
日期:2015-05 浏览次数:3519
日期:2018-09 浏览次数:3305
日期:2018-06 浏览次数:3433
日期:2017-02 浏览次数:3869
日期:2018-02 浏览次数:4334
日期:2018-02 浏览次数:4173
日期:2016-12 浏览次数:3571
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.