允许Java使用不受信任的证书进行SSLHTTPS连接

允许Java使用不受信任的证书进行SSLHTTPS连接,第1张

允许Java使用不受信任的证书进行SSL / HTTPS连接

这是一些相关的代码

// Create a trust manager that does not validate certificate chainsTrustManager[] trustAllCerts = new TrustManager[]{    new X509TrustManager() {        public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null;        }        public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {        }        public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {        }    }};// Install the all-trusting trust managertry {    SSLContext sc = SSLContext.getInstance("SSL");    sc.init(null, trustAllCerts, new java.security.SecureRandom());    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());} catch (Exception e) {}// Now you can access an https URL without having the certificate in the truststoretry {    URL url = new URL("https://hostname/index.html");} catch (MalformedURLException e) {}

这将完全禁用SSL检查-只是不要从此类代码中学习异常处理!

要执行你想要的 *** 作,你必须在TrustManager中实施提示用户的检查。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5004199.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存