android中webView的https证书校验以及基于okhttp的接口https证书校验

webView证书校验:

通过chrome浏览器拿到证书cer文件

获取证书公钥

    public void readX509CerFile() {
        try {
            InputStream inStream = getAssets().open("ccc.cer");
            // 创建X509工厂类
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            // 创建证书对象
            X509Certificate oCert = (X509Certificate) cf
                    .generateCertificate(inStream);
            inStream.close();
            String info = null;
            // 获得证书版本
            Log.d(TAG, "publicKey: " + oCert.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

证书公钥类

public class SslCert {

    public static final String[] keys = new String[]{
            "30820122300d06092a864886f70d01010105000382010f003082010a0282010100证书公钥0203010001"
    };
}

判断证书接口类

public class SslInterfance {


    public interface SslInterfase {
        void signSslResult(boolean result);
    }

    private static SslInterfase mSslInterfase;


    public static void setSslInterfase(SslInterfase sslInterfase) {
        mSslInterfase = sslInterfase;
    }


    public static void getUnsafeFromService(String url) {
        String[] publicKeys = SslCert.keys;
        try {
            X509TrustManager[] trustManagers = new X509TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                        }

                        @Override
                        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                            if (x509Certificates == null) {
                                throw new IllegalArgumentException("check Service x509Certificates is null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值