8000 :art: 优化部分代码 · Goforit000/WxJava@12b83af · GitHub
[go: up one dir, main page]

Skip to content

Commit 12b83af

Browse files
committed
🎨 优化部分代码
1 parent c483d6f commit 12b83af

File tree

1 file changed

+11
-15
lines changed
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config

1 file changed

+11
-15
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import lombok.EqualsAndHashCode;
1111
import lombok.SneakyThrows;
1212
import lombok.ToString;
13+
import lombok.extern.slf4j.Slf4j;
1314
import org.apache.commons.lang3.RegExUtils;
1415
import org.apache.commons.lang3.StringUtils;
1516
import org.apache.http.impl.client.CloseableHttpClient;
@@ -32,6 +33,7 @@
3233
* @author Binary Wang (<a href="https://github.com/binarywang">...</a>)
3334
*/
3435
@Data
36+
@Slf4j
3537
@ToString(exclude = "verifier")
3638
@EqualsAndHashCode(exclude = "verifier")
3739
public class WxPayConfig {
@@ -253,7 +255,7 @@ public SSLContext initSSLContext() throws WxPayException {
253255

254256
/**
255257
* 初始化api v3请求头 自动签名验签
256-
* 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
258+
* 方法参照 <a href="https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient">微信支付官方api项目</a>
257259
*
258260
* @return org.apache.http.impl.client.CloseableHttpClient
259261
* @author doger.wang
@@ -397,8 +399,8 @@ private InputStream loadConfigInputStream(String configPath) throws WxPayExcepti
397399
if (!file.exists()) {
398400
throw new WxPayException(fileNotFoundMsg);
399401
}
400-
401-
// return Files.newInputStream(file.toPath());
402+
//使用Files.newInputStream打开公私钥文件,会存在无法释放句柄的问题
403+
//return Files.newInputStream(file.toPath());
402404
return new FileInputStream(file);
403405
} catch (IOException e) {
404406
throw new WxPayException(fileHasProblemMsg, e);
@@ -408,36 +410,30 @@ private InputStream loadConfigInputStream(String configPath) throws WxPayExcepti
408410

409411
/**
410412
* 分解p12证书文件
411-
*
412-
* @return
413413
*/
414414
private Object[] p12ToPem() {
415415
String key = getMchId();
416416
if (StringUtils.isBlank(key)) {
417417
return null;
418418
}
419+
419420
// 分解p12证书文件
420-
PrivateKey privateKey = null;
421-
X509Certificate x509Certificate = null;
422421
try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(),
423-
this.keyContent, "p12证书");){
424-
if (inputStream == null) {
425-
return null;
426-
}
422+
this.keyContent, "p12证书");) {
427423
KeyStore keyStore = KeyStore.getInstance("PKCS12");
428424
keyStore.load(inputStream, key.toCharArray());
429425

430426
String alias = keyStore.aliases().nextElement();
431-
privateKey = (PrivateKey) keyStore.getKey(alias, key.toCharArray());
427+
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, key.toCharArray());
432428

433429
Certificate certificate = keyStore.getCertificate(alias);
434-
x509Certificate = (X509Certificate) certificate;
430+
X509Certificate x509Certificate = (X509Certificate) certificate;
435431
return new Object[]{privateKey, x509Certificate};
436432
} catch (Exception e) {
437-
e.printStackTrace();
433+
log.error("加载证书时发生异常", e);
438434
}
439-
return null;
440435

436+
return null;
441437

442438
}
443439
}

0 commit comments

Comments
 (0)
0