常量文件定义:
1
public static final String APICLIENT_CERT_P12 = "apiclient_cert.p12";
2
使用资源文件:
1
CloseableHttpClient httpclient = null;
2
try {
3
KeyStore keyStore = KeyStore.getInstance("PKCS12");
4
//类加载器
5
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
6
//以流的形式读取资源文件
7
InputStream inputStream = classLoader.getResourceAsStream(GlobalConfig.APICLIENT_CERT_P12);
8
log.info("======apiclient_cert_path===end===");
9
try {
10
//装载证书资源文件
11
keyStore.load(inputStream, partner.toCharArray());
12
} catch (IOException e) {
13
log.info("======FileInputStream===IOException:" + e);
14
e.printStackTrace();
15
return false;
16
} finally {
17
inputStream.close();
18
}
19
// 这里也是写密码的
20
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, partner.toCharArray())
21
22
.build();
23
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
24
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
25
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
26
} catch (Exception e) {
27
e.printStackTrace();
28
}