package com.hx.mp.util; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContexts; import javax.net.ssl.SSLContext; import java.io.File; import java.io.FileInputStream; import java.security.KeyStore; @SuppressWarnings("deprecation") public class CertUtil { /** * 加载证书 */ public static SSLConnectionSocketFactory initCert(String certPath, String mchId) throws Exception { FileInputStream instream = null; KeyStore keyStore = KeyStore.getInstance("PKCS12"); instream = new FileInputStream(new File(certPath)); keyStore.load(instream, mchId.toCharArray()); if (null != instream) { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,mchId.toCharArray()).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); return sslsf; } }