E1ED922C1E9526DD63272D7EC5C6CB77
2020-11-02 b2fdfd80054cadaafd394b624224db3803933180
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
    }
}