android4.0以上webview加载SSl证书兼容性问题

下面参考了其他博客获取证书。
public class AppConfig
{

	private static WebView mWebView;

	public static X509Certificate[] mX509Certificates;

	public static PrivateKey mPrivateKey;

	public static String CERTFILE_PASSWORD;

	private static void clearParent()
	{
		if (mWebView != null)
		{
			ViewGroup p = (ViewGroup) mWebView.getParent();
			if (p != null)
			{
				p.removeAllViewsInLayout();
			}
		}
	}

	public static void initPrivateKeyAndX509Certificate(Context c)
			throws Exception
	{
		if (mPrivateKey != null && mX509Certificates != null)
		{
			return;
		}
		KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
		keyStore.load(c.getResources().openRawResource(R.raw.dianshang),
				CERTFILE_PASSWORD.toCharArray());
		Enumeration<?> localEnumeration;
		localEnumeration = keyStore.aliases();
		while (localEnumeration.hasMoreElements())
		{
			String str3 = (String) localEnumeration.nextElement();
			mPrivateKey =
					(PrivateKey) keyStore.getKey(str3,
							CERTFILE_PASSWORD.toCharArray());
			if (mPrivateKey == null)
			{
				continue;
			}
			else
			{
				Certificate[] arrayOfCertificate =
						keyStore.getCertificateChain(str3);
				mX509Certificates =
						new X509Certificate[arrayOfCertificate.length];
				for (int j = 0; j < mX509Certificates.length; j++)
				{
					mX509Certificates[j] =
							((X509Certificate) arrayOfCertificate[j]);
				}
			}
		}
	}

	public static void initPrivateKeyAndX509Certificate(Context c,
			InputStream in) throws Exception
	{
		if (mPrivateKey != null && mX509Certificates != null)
		{
			return;
		}
		KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
		keyStore.load(in, CERTFILE_PASSWORD.toCharArray());
		Enumeration<?> localEnumeration;
		localEnumeration = keyStore.aliases();
		while (localEnumeration.hasMoreElements())
		{
			String str3 = (String) localEnumeration.nextElement();
			mPrivateKey =
					(PrivateKey) keyStore.getKey(str3,
							CERTFILE_PASSWORD.toCharArray());
			if (mPrivateKey == null)
			{
				continue;
			}
			else
			{
				Certificate[] arrayOfCertificate =
						keyStore.getCertificateChain(str3);
				mX509Certificates =
						new X509Certificate[arrayOfCertificate.length];
				for (int j = 0; j < mX509Certificates.length; j++)
				{
					mX509Certificates[j] =
							((X509Certificate) arrayOfCertificate[j]);
				}
			}
		}
	}}


下面是复写WebViewClient或者WebViewClientClassicExt()具体兼容性问题可以参考下面类的注释:

/**
 * 4.0-4.1可以继承WebViewClient类实现下面的方法(4.1以上不兼容,执行不了类里面的加载证书的方法)
 * 但是在4.1以上上面的方法不可用,必须继承WebViewClientClassicExt方可实现证书加载(4.1以上部分方法的调用放到这个类里面了)
 * @author dennis
 *
 */
public class AppWebViewClientEx extends WebViewClientClassicExt
{

	public AppWebViewClientEx(Context c)
	{
	}

	public void onReceivedClientCertRequest(WebView view,
			ClientCertRequestHandler handler, String host_and_port)
	{
		if ((null != AppConfig.mPrivateKey)
				&& ((null != AppConfig.mX509Certificates) && (AppConfig.mX509Certificates.length != 0)))
		{
			handler.proceed(AppConfig.mPrivateKey, AppConfig.mX509Certificates);
		}
		else
		{
			handler.cancel();
		}
	}

	@Override
	public void onReceivedSslError(final WebView view, SslErrorHandler handler,
			SslError error)
	{
		handler.proceed();
	}
}
使用隐藏api:
这个问题弄了好久终于搞定了4.0以上webview加载ssl证书不兼容问题....纠结死了....

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值