publicstatic SSLSocketFactory getSSLSocketFactory()throws Exception { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = newTrustManager[]{newX509TrustManager() { //证书中的公钥 publicstaticfinalStringPUB_KEY="输入您的公钥";
if (!(chain.length > 0)) { thrownewIllegalArgumentException("checkServerTrusted: X509Certificate is empty"); }
if (!(null != authType && authType.equalsIgnoreCase("RSA"))) { thrownewCertificateException("checkServerTrusted: AuthType is not RSA"); }
// Perform customary SSL/TLS checks try { TrustManagerFactorytmf= TrustManagerFactory.getInstance("X509"); tmf.init((KeyStore) null); for (TrustManager trustManager : tmf.getTrustManagers()) { ((X509TrustManager) trustManager).checkServerTrusted(chain, authType); } } catch (Exception e) { thrownewCertificateException(e); } // Hack ahead: BigInteger and toString(). We know a DER encoded Public Key begins // with 0×30 (ASN.1 SEQUENCE and CONSTRUCTED), so there is no leading 0×00 to drop. RSAPublicKeypubkey= (RSAPublicKey) chain[0].getPublicKey();