From 12b83affe4f22755a5407148063af4fd31c99707 Mon Sep 17 00:00:00 2001
From: Binary Wang
Date: Thu, 31 Oct 2024 16:03:55 +0800
Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../binarywang/wxpay/config/WxPayConfig.java | 26 ++++++++-----------
1 file changed, 11 insertions(+), 15 deletions(-)
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java
index 3bc868d07..932fa323e 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java
@@ -10,6 +10,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import lombok.ToString;
+import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
@@ -32,6 +33,7 @@ import java.util.Optional;
* @author Binary Wang (...)
*/
@Data
+@Slf4j
@ToString(exclude = "verifier")
@EqualsAndHashCode(exclude = "verifier")
public class WxPayConfig {
@@ -253,7 +255,7 @@ public class WxPayConfig {
/**
* 初始化api v3请求头 自动签名验签
- * 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
+ * 方法参照 微信支付官方api项目
*
* @return org.apache.http.impl.client.CloseableHttpClient
* @author doger.wang
@@ -397,8 +399,8 @@ public class WxPayConfig {
if (!file.exists()) {
throw new WxPayException(fileNotFoundMsg);
}
-
-// return Files.newInputStream(file.toPath());
+ //使用Files.newInputStream打开公私钥文件,会存在无法释放句柄的问题
+ //return Files.newInputStream(file.toPath());
return new FileInputStream(file);
} catch (IOException e) {
throw new WxPayException(fileHasProblemMsg, e);
@@ -408,36 +410,30 @@ public class WxPayConfig {
/**
* 分解p12证书文件
- *
- * @return
*/
private Object[] p12ToPem() {
String key = getMchId();
if (StringUtils.isBlank(key)) {
return null;
}
+
// 分解p12证书文件
- PrivateKey privateKey = null;
- X509Certificate x509Certificate = null;
try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(),
- this.keyContent, "p12证书");){
- if (inputStream == null) {
- return null;
- }
+ this.keyContent, "p12证书");) {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(inputStream, key.toCharArray());
String alias = keyStore.aliases().nextElement();
- privateKey = (PrivateKey) keyStore.getKey(alias, key.toCharArray());
+ PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, key.toCharArray());
Certificate certificate = keyStore.getCertificate(alias);
- x509Certificate = (X509Certificate) certificate;
+ X509Certificate x509Certificate = (X509Certificate) certificate;
return new Object[]{privateKey, x509Certificate};
} catch (Exception e) {
- e.printStackTrace();
+ log.error("加载证书时发生异常", e);
}
- return null;
+ return null;
}
}