1.通过inputStream直接加载证书流(需要放到项目某一位置)
为了避免windows和linux区别直接用
SpringBootApplication.Class.getResourceAsStream("/apiclient_cert.p12")
获取文件流传入需要携带证书请求方式中进行微信服务器交互
/**
* 需要证书
*
* @param
* @return XML字符串
* @throws Exception
*/
public static String doWxpayRequest(String httpurl, String strxml, String mch_id, InputStream in) throws Exception {
CloseableHttpClient client = null;
HttpPost httpPost = null;
try {
KeyStore clientTrustKeyStore = KeyStore.getInstance("PKCS12");
clientTrustKeyStore.load(in, mch_id.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientTrustKeyStore, mch_id.toCharArray());
TrustManager[] tm = {new MyX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(kmf.getKeyManagers(), tm, new java.security.SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
httpPost = new HttpPost(httpurl);
httpPost.setEntity(new StringEntity(strxml, "utf-8"));
CloseableHttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() == 200) {
return EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (client != null) {
client.close();
}
}
return null;
}
该方式适用于代码未分模块的方式,并且可能导致io读取阻塞。
2.把证书读成字节数组转为16进制存储
代码如下,加载证书文件流,通过hex解析为16进制存到静态变量里
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream("D://apiclient_cert.p12");
String s1 = Hex.encodeHexString(StreamUtils.copyToByteArray(file));
System.out.println(s1);
} catch (Exception e) {
e.printStackTrace();
}
}
将加密的16进制证书码传入携带证书方法请求方式进行微信服务器交互
public static String doWxpayRequest(String httpurl, String strxml, String mch_id, String certCode) throws Exception {
CloseableHttpClient client = null;
HttpPost httpPost = null;
try {
// 解密出16进制原证书文件内容为字节数组
byte[] bytes = Hex.decodeHex(certCode.toCharArray());
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
KeyStore clientTrustKeyStore = KeyStore.getInstance("PKCS12");
clientTrustKeyStore.load(input, mch_id.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientTrustKeyStore, mch_id.toCharArray());
TrustManager[] tm = {new MyX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(kmf.getKeyManagers(), tm, new java.security.SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
httpPost = new HttpPost(httpurl);
httpPost.setEntity(new StringEntity(strxml, "utf-8"));
CloseableHttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() == 200) {
return EntityUtils.toString(entity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (client != null) {
client.close();
}
}
return null;
}
这种存储方式适合更多场景,代码分不分模块都支持。