//商户Id
@Value("${payConfig.mchId}")
private String key;
//商户证书路径
@Value("${payConfig.payConfigPath}")
private String payConfigPath;
/**
* 加载证书
*/
private CloseableHttpClient initCert(HttpServletRequest request) throws Exception {
// 商户证书的路径
// 证书文件路径
String path = request.getServletContext().getRealPath(payConfigPath);
// 指定读取证书格式为PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// 读取本机存放的PKCS12证书文件
FileInputStream instream = new FileInputStream(new File(path));
try {
PluginConfig pluginConfig = weixinPublicPaymentPlugin.getPluginConfig();
// 微信商户号
String mchId = pluginConfig.getAttribute("mchId");
// 指定PKCS12的密码(商户ID)
keyStore.load(instream, mchId.toCharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();
// 指定TLS版本
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
// 设置httpclient的SSLSocketFactory
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}