Skip to content

Commit

Permalink
🐛 #3392【微信支付】修复V3客户端初始化时p12证书加载失败的问题
Browse files Browse the repository at this point in the history
LeungHL authored Oct 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent bf7356e commit c483d6f
Showing 1 changed file with 8 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -270,6 +270,7 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
if (objects != null) {
merchantPrivateKey = (PrivateKey) objects[0];
certificate = (X509Certificate) objects[1];
this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
}
try {
if (merchantPrivateKey == null) {
@@ -405,38 +406,24 @@ private InputStream loadConfigInputStream(String configPath) throws WxPayExcepti
}
}

/**
* 从配置路径 加载p12证书文件流
*
* @return 文件流
*/
private InputStream loadP12InputStream() {
try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(),
this.keyContent, "p12证书");) {
return inputStream;
} catch (Exception e) {
return null;
}
}

/**
* 分解p12证书文件
*
* @return
*/
private Object[] p12ToPem() {
InputStream inputStream = this.loadP12InputStream();
if (inputStream == null) {
return null;
}
String key = getMchId();
if (StringUtils.isBlank(key)) {
return null;
}
// 分解p12证书文件
PrivateKey privateKey = null;
X509Certificate x509Certificate = null;
try {
try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(),
this.keyContent, "p12证书");){
if (inputStream == null) {
return null;
}
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(inputStream, key.toCharArray());

@@ -446,8 +433,8 @@ private Object[] p12ToPem() {
Certificate certificate = keyStore.getCertificate(alias);
x509Certificate = (X509Certificate) certificate;
return new Object[]{privateKey, x509Certificate};
} catch (Exception ignored) {

} catch (Exception e) {
e.printStackTrace();
}
return null;

0 comments on commit c483d6f

Please sign in to comment.