因为网页中要用到自定义SSL证书,而且密钥是 xxx.p12,
我们是将密钥转化为byte[]的形式传进来的,传入密码,这样来处理SSL
关键在onReceivedClientCertRequest这个方法,要用到隐藏类,因此得用编译android出来的class.jar,不然识别不了该隐藏类
所以自定义WebViewClient如下:
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import android.content.Context;
import android.net.http.SslError;
import android.webkit.ClientCertRequestHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SSLWebViewClient extends WebViewClient{
private X509Certificate[] certificatesChain;
private PrivateKey clientCertPrivateKey;
byte[] certfile_p12;
private String certfile_password = "";
public SSLWebViewClient(Context context, byte[] cert, String pass) throws Exception {
super();
certfile_password = pass;
certfile_p12 = cert;
initPrivateKeyAndX509Certificate();
}
private void initPrivateKeyAndX509Certificate() throws Exception {
KeyStore keyStore;
keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(DataTools.byteTOInputStream(certfile_p12), certfile_password.toCharArray());
Enumeration<?> localEnumeration;
localEnumeration = keyStore.aliases();
while (localEnumeration.hasMoreElements()) {
String str3 = (String) localEnumeration.nextElement();
clientCertPrivateKey = (PrivateKey) keyStore.getKey(str3, certfile_password.toCharArray());
if (clientCertPrivateKey == null) {
continue;
} else {
Certificate[] arrayOfCertificate = keyStore.getCertificateChain(str3);
certificatesChain = new X509Certificate[arrayOfCertificate.length];
for (int j = 0; j < certificatesChain.length; j++) {
certificatesChain[j] = ((X509Certificate) arrayOfCertificate[j]);
}
}
}
}
public void onReceivedClientCertRequest(WebView view,
ClientCertRequestHandler handler, String host_and_port) {
if((null != clientCertPrivateKey) && ((null!=certificatesChain) && (certificatesChain.length !=0))){
handler.proceed(this.clientCertPrivateKey, this.certificatesChain);
}else{
handler.cancel();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedSslError(final WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
}
}
其中用到转换工具类,如下:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataTools {
final static int BUFFER_SIZE = 4096;
/**
* 将InputStream转换成String
* @param in InputStream
* @return String
* @throws Exception
*
*/
public static String InputStreamTOString(InputStream in) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(),"ISO-8859-1");
}
/**
* 将InputStream转换成某种字符编码的String
* @param in
* @param encoding
* @return
* @throws Exception
*/
public static String InputStreamTOString(InputStream in,String encoding) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(),"ISO-8859-1");
}
/**
* 将String转换成InputStream
* @param in
* @return
* @throws Exception
*/
public static InputStream StringTOInputStream(String in) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
return is;
}
/**
* 将InputStream转换成byte数组
* @param in InputStream
* @return byte[]
* @throws IOException
*/
public static byte[] InputStreamTOByte(InputStream in) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
}
/**
* 将byte数组转换成InputStream
* @param in
* @return
* @throws Exception
*/
public static InputStream byteTOInputStream(byte[] in) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(in);
return is;
}
/**
* 将byte数组转换成String
* @param in
* @return
* @throws Exception
*/
public static String byteTOString(byte[] in) throws Exception{
InputStream is = byteTOInputStream(in);
return InputStreamTOString(is);
}
}
需要用到android便宜出来的class的jar包,已经编译好的放在raw文件夹里,目录结构如下:
编译出来的 sslwebviewclient.jar 就可以在你的项目中使用了。
使用方法如下:
WebView browser = (WebView)findViewById(R.id.id_browser);
try {
browser.setWebViewClient(new SSLWebViewClient(this, AndroidNaviAPP.getInstance().getPfx(), AndroidNaviAPP.getInstance().getPass()));
} catch (Exception e) {
//异常处理
}
这样就可以正常打开,自定义密钥的SSL网页了
可能用到的资源文件(包含编译的class.jar 和 我本地生成好的sslwebviewclient.jar,可以直接用,最好自己编):
http://download.csdn.net/detail/zoeice/6485653
分享到:
相关推荐
android webview SSL认证配置(p12证书)
自定义WebView页面中,长按文本的弹出选项、点击选择后,分享、转发、收藏选择文本
自定义的WebView,可以自动适应屏幕的大小,可以监听网页上的点击事件
主要介绍了Android APP之WebView校验SSL证书的方法,需要的朋友可以参考下
android 腾讯微博Oauth2.0认证以及自定义webview认证
android ssl证书验证
Android应用源码之webview重载使用&自定义网址.zip
webview加载失败,自定义错误页面处理
主要介绍了Android WebView自定义长按选择实现收藏/分享选中文本功能,需要的朋友可以参考下
自定义webview中的网页“拍照”按钮调用系统相机拍照的API接口MyBrowserAPI.CapturePhoto(filename),在网页JavaScript中调用以后返回存放照片的完整路径名,默认将照片存放在系统SD卡的根目录,包含整个工程中需要添加...
一些项目中要用到WebView调用网页,然而又没在进度条或者加载动画,每次都要去写,觉得麻烦,本人写了LJWebView,目前DEMO中加入了两种加载动画供大家选择,只需在代码中调用setProgressStyle即可,默认为水平进度条...
自定义了一个webview 实现了进度条显示、在同一个webview中打开多链接以及页面的前进后退
webview重载使用&自定义网址
webview播放视频代码 可全屏 添加自定义vedioplayer播放器播放
自定义webView,实现自己想要的授权网页显示方式
Android webview重载使用&自定义网址
就是自己画一条进度线(大家应该都会吧)然后加载到WebView的上面,开始进度条是隐藏的,进度线初始值为1,然后为了效果好一点,初始少于10的进度都让它加载到10的位置,等进度到100的时候0.2秒后隐藏。 请记得添加...
Android Webview上的ssl warning的处理方式详解 前言: 因为最近遇到google pay上汇报的安全漏洞问题,需要处理ssl warning. 安全提醒 您的应用中 WebViewClient.onReceivedSslError 处理程序的实施方式很不安全。...
前言 最近公司项目需求,在项目中嵌入h5页面,一般原生,看着感觉跟往常一样,一个地址就完全ok了,如果是这样那就没有这个博文的必要了! 项目的登录使用的token登录,在移动端的登录是原生的,但是h5也是有登录...