{"id":1631,"date":"2023-03-25T21:26:50","date_gmt":"2023-03-25T13:26:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1631"},"modified":"2023-04-23T22:01:36","modified_gmt":"2023-04-23T14:01:36","slug":"how-to-verify-ssl-certificates-in-webview-of-android-app","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/how-to-verify-ssl-certificates-in-webview-of-android-app\/","title":{"rendered":"Android APP\u4e4bWebView\u5982\u4f55\u6821\u9a8cSSL\u8bc1\u4e66"},"content":{"rendered":"<p>Android\u7cfb\u7edf\u7684\u788e\u7247\u5316\u5f88\u4e25\u91cd\uff0c\u5e76\u4e14\u624b\u673a\u65e5\u671f\u4e0d\u6b63\u786e\u3001\u624b\u673a\u6839\u8bc1\u4e66\u5f02\u5e38\u3001WebView BUG\u7b49\u5404\u79cd\u539f\u56e0\uff0c\u90fd\u4f1a\u5bfc\u81f4<code>WebViewClient<\/code>\u65e0\u6cd5\u8bbf\u95eeHTTPS\u7ad9\u70b9\u3002SSL\u9519\u8bef\u7684\u5904\u7406\u65b9\u5f0f\u5341\u5206\u5173\u952e\uff0c\u5982\u679c\u5904\u7406\u4e0d\u5f53\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e2d\u95f4\u4eba\u653b\u51fb\uff0c\u9ed1\u5ba2\u7a83\u542c\u6570\u636e\uff0c\u8fdb\u800c\u5f15\u53d1\u5b89\u5168\u4e8b\u6545\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4e25\u8c28\u5730\u5904\u7406<code>onReceivedSslError<\/code>\u5c24\u4e3a\u91cd\u8981\u3002\u8bf7\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\uff0c\u539f\u7406\u662f\uff1a\u5982\u679cWebView\u62a5\u544aSSL\u9519\u8bef\uff0c\u7a0b\u5e8f\u5c06\u4f1a\u5bf9\u670d\u52a1\u5668\u8bc1\u4e66\u8fdb\u884c\u5f3a\u6821\u9a8c\uff0c\u5982\u679c\u670d\u52a1\u5668\u4f20\u5165\u8bc1\u4e66\u7684\u6307\u7eb9(sha256)\u4e0e\u8bb0\u5f55\u503c\u4e00\u81f4\uff0c\u8bf4\u660eWebView\u9a8c\u8bc1\u8fc7\u7a0b\u5b58\u5728\u7f3a\u9677(\u5982\uff1a\u624b\u673a\u65e5\u671f\u9519\u8bef\u3001\u6839\u8bc1\u4e66\u88ab\u5220\u9664 \u7b49)\uff0c\u5ffd\u7565SSL\u9519\u8bef\uff1b\u5982\u679c\u8bc1\u4e66\u5339\u914d\u5931\u8d25\uff0c\u8868\u660e\u6570\u636e\u901a\u4fe1\u6709\u95ee\u9898\uff0c\u4fdd\u7559\u963b\u65ad\u3002<\/p>\n<p>\u8bf7\u5148\u70b9\u51fb <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.myssl.cn\/tools\/downloadchain.html\" title=\"\u8fd9\u91cc\">\u8fd9\u91cc<\/a>\uff0c\u83b7\u53d6\u8bc1\u4e66\u7684\u6307\u7eb9(sha256)\uff0c\u7136\u540e\u8c03\u6574\u4ee3\u7801\u4e2d\u7684SHA256\u6570\u7ec4\u53d8\u91cf\u3002\u5982\u679cAPP\u9700\u8981\u8bbf\u95ee\u591a\u5f20\u8bc1\u4e66\uff0c\u8bf7\u5728\u4ee3\u7801\u4e2d\u52a0\u5165\u591a\u4e2a\u8bc1\u4e66\u6307\u7eb9\u6570\u503c\u3002\u5728\u6d4b\u8bd5\u4ee3\u7801\u65f6\uff0c\u8bf7\u5c06\u624b\u673a\u65e5\u671f\u8bbe\u7f6e\u5728\u8bc1\u4e66\u6709\u6548\u671f\u4e4b\u524d\uff0c\u5224\u65adWebView\u662f\u5426\u80fd\u6b63\u5e38\u8bbf\u95eeHTTPS\u7ad9\u70b9\u3002<\/p>\n<pre><code class=\"language-java\">webview.setWebViewClient(new WebViewClient() {\n    @Override\n    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {\n        if (error.getPrimaryError() == SslError.SSL_DATE_INVALID  \/\/ \u65e5\u671f\u4e0d\u6b63\u786e\n            || error.getPrimaryError() == SslError.SSL_EXPIRED \/\/ \u65e5\u671f\u4e0d\u6b63\u786e\n            || error.getPrimaryError() == SslError.SSL_INVALID \/\/ WebView BUG\n            || error.getPrimaryError() == SslError.SSL_UNTRUSTED) { \/\/ \u6839\u8bc1\u4e66\u4e22\u5931\n            if (chkMySSLCNCert(error.getCertificate())) {\n                handler.proceed();  \/\/ \u5982\u679c\u8bc1\u4e66\u4e00\u81f4\uff0c\u5ffd\u7565\u9519\u8bef\n            }\n        }\n    }\n\n    private boolean chkMySSLCNCert(SslCertificate cert) {\n        byte[] SHA256 = { 35, 76, 110, -121, -68, -104, -12, 84, 39, 119, -55,\n            101, 95, -8, -90, 9, 36, -108, 5, -57, 76, -98, -19, -73, 91, -37, 18,\n            64, 32, -41, 0, 109 };  \/\/\u8bc1\u4e66\u6307\u7eb9\n        Bundle bundle = SslCertificate.saveState(cert);\n        byte[] bytes = bundle.getByteArray(&quot;x509-certificate&quot;);\n        if (bytes != null) {\n            try {\n                CertificateFactory cf = CertificateFactory.getInstance(&quot;X.509&quot;);  \n                Certificate ca = cf.generateCertificate(new ByteArrayInputSteam(bytes));  \n                MessageDigest sha256 = MessageDigest.getInstance(&quot;SHA-256&quot;);\n                byte[] Key = sha256.digest(((X509Certificate) ca).getEncoded());\n                return Arrays.equals(key, MySSLCNSHA256);\n            } catch (Exception Ex) {}\n        }\n        return false;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android\u7cfb\u7edf\u7684\u788e\u7247\u5316\u5f88\u4e25\u91cd\uff0c\u5e76\u4e14\u624b\u673a\u65e5\u671f\u4e0d\u6b63\u786e\u3001\u624b\u673a\u6839\u8bc1\u4e66\u5f02\u5e38\u3001WebView BUG\u7b49\u5404\u79cd\u539f\u56e0\uff0c\u90fd\u4f1a\u5bfc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[67,407],"class_list":["post-1631","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-webview","tag-407"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1631"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1631\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}