chenjiahe
2023-02-07 1b181507f2564bfd327cc3a8e5d75a8620c3d09d
提交 | 用户 | age
5c5945 1 package com.hx.mp.util;
E 2
3 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
4 import org.apache.http.conn.ssl.SSLContexts;
5
6 import javax.net.ssl.SSLContext;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.security.KeyStore;
10
11 @SuppressWarnings("deprecation")
12 public class CertUtil {
13     /**
14      * 加载证书
15      */
16     public static SSLConnectionSocketFactory initCert(String certPath, String mchId) throws Exception {
17         FileInputStream instream = null;
18         KeyStore keyStore = KeyStore.getInstance("PKCS12");
19         instream = new FileInputStream(new File(certPath));
20         keyStore.load(instream, mchId.toCharArray());
21
22         if (null != instream) {
23             instream.close();
24         }
25
26         SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,mchId.toCharArray()).build();
27         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
28
29         return sslsf;
30     }
31 }