content: package com.jingewenku.abrahamcaijin.commonutil;
  
  import javax.net.ssl.*;
  import java.io.IOException;
  import java.io.InputStream;
  import java.security.*;
  import java.security.cert.CertificateException;
  import java.security.cert.CertificateFactory;
  
  /**
   * @Description:主要功能: Https 证书工具类
   * @Prject: CommonUtilLibrary
   * @Package: com.jingewenku.abrahamcaijin.commonutil
   * @author: AbrahamCaiJin
   * @date: 2017年07月21日 15:28
   * @Copyright: 个人版权所有
   * @Company:
   * @version: 1.0.0
   */
  
  public class SSLUtil {
      //使用命令keytool -printcert -rfc -file srca.cer 导出证书为字符串，然后将字符串转换为输入流，如果使用的是okhttp可以直接使用new Buffer().writeUtf8(s).inputStream()
  
      /**
       * 返回SSLSocketFactory
       *
       * @param certificates 证书的输入流
       * @return SSLSocketFactory
       */
      public static SSLSocketFactory getSSLSocketFactory(InputStream... certificates) {
          return getSSLSocketFactory(null,certificates);
      }
  
      /**
       * 双向认证
       * @param keyManagers KeyManager[]
       * @param certificates 证书的输入流
       * @return SSLSocketFactory
       */
      public static SSLSocketFactory getSSLSocketFactory(KeyManager[] keyManagers, InputStream... certificates) {
          try {
              CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
              KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
              keyStore.load(null);
              int index = 0;
              for (InputStream certificate : certificates) {
                  String certificateAlias = Integer.toString(index++);
                  keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
                  try {
                      if (certificate != null)
                          certificate.close();
                  } catch (IOException e) {
                  }
              }
              SSLContext sslContext = SSLContext.getInstance("TLS");
              TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
              trustManagerFactory.init(keyStore);
              sslContext.init(keyManagers, trustManagerFactory.getTrustManagers(), new SecureRandom());
              SSLSocketFactory socketFactory = sslContext.getSocketFactory();
              return socketFactory;
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      /**
       * 获得双向认证所需的参数
       * @param bks bks证书的输入流
       * @param keystorePass 秘钥
       * @return KeyManager[]对象
       */
      public static KeyManager[] getKeyManagers(InputStream bks, String keystorePass) {
          KeyStore clientKeyStore = null;
          try {
              clientKeyStore = KeyStore.getInstance("BKS");
              clientKeyStore.load(bks, keystorePass.toCharArray());
              KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
              keyManagerFactory.init(clientKeyStore, keystorePass.toCharArray());
              KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
              return keyManagers;
          } catch (KeyStoreException e) {
              e.printStackTrace();
          } catch (UnrecoverableKeyException e) {
              e.printStackTrace();
          } catch (CertificateException e) {
              e.printStackTrace();
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  }