10
10
import lombok .EqualsAndHashCode ;
11
11
import lombok .SneakyThrows ;
12
12
import lombok .ToString ;
13
+ import lombok .extern .slf4j .Slf4j ;
13
14
import org .apache .commons .lang3 .RegExUtils ;
14
15
import org .apache .commons .lang3 .StringUtils ;
15
16
import org .apache .http .impl .client .CloseableHttpClient ;
32
33
* @author Binary Wang (<a href="https://github.com/binarywang">...</a>)
33
34
*/
34
35
@ Data
36
+ @ Slf4j
35
37
@ ToString (exclude = "verifier" )
36
38
@ EqualsAndHashCode (exclude = "verifier" )
37
39
public class WxPayConfig {
@@ -253,7 +255,7 @@ public SSLContext initSSLContext() throws WxPayException {
253
255
254
256
/**
255
257
* 初始化api v3请求头 自动签名验签
256
- * 方法参照微信官方https ://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
258
+ * 方法参照 <a href="https ://github.com/wechatpay-apiv3/wechatpay-apache-httpclient">微信支付官方api项目</a>
257
259
*
258
260
* @return org.apache.http.impl.client.CloseableHttpClient
259
261
* @author doger.wang
@@ -397,8 +399,8 @@ private InputStream loadConfigInputStream(String configPath) throws WxPayExcepti
397
399
if (!file .exists ()) {
398
400
throw new WxPayException (fileNotFoundMsg );
399
401
}
400
-
401
- // return Files.newInputStream(file.toPath());
402
+ //使用Files.newInputStream打开公私钥文件,会存在无法释放句柄的问题
403
+ // return Files.newInputStream(file.toPath());
402
404
return new FileInputStream (file );
403
405
} catch (IOException e ) {
404
406
throw new WxPayException (fileHasProblemMsg , e );
@@ -408,36 +410,30 @@ private InputStream loadConfigInputStream(String configPath) throws WxPayExcepti
408
410
409
411
/**
410
412
* 分解p12证书文件
411
- *
412
- * @return
413
413
*/
414
414
private Object [] p12ToPem () {
415
415
String key = getMchId ();
416
416
if (StringUtils .isBlank (key )) {
417
417
return null ;
418
418
}
419
+
419
420
// 分解p12证书文件
420
- PrivateKey privateKey = null ;
421
- X509Certificate x509Certificate = null ;
422
421
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证书" );) {
427
423
KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
428
424
keyStore .load (inputStream , key .toCharArray ());
429
425
430
426
String alias = keyStore .aliases ().nextElement ();
431
- privateKey = (PrivateKey ) keyStore .getKey (alias , key .toCharArray ());
427
+ PrivateKey privateKey = (PrivateKey ) keyStore .getKey (alias , key .toCharArray ());
432
428
433
429
Certificate certificate = keyStore .getCertificate (alias );
434
- x509Certificate = (X509Certificate ) certificate ;
430
+ X509Certificate x509Certificate = (X509Certificate ) certificate ;
435
431
return new Object []{privateKey , x509Certificate };
436
432
} catch (Exception e ) {
437
- e . printStackTrace ( );
433
+ log . error ( "加载证书时发生异常" , e );
438
434
}
439
- return null ;
440
435
436
+ return null ;
441
437
442
438
}
443
439
}
0 commit comments