From 1812b233bbcccd12944aade7188f042c622cf26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=B5=A9?= Date: Mon, 17 Aug 2020 13:32:42 +0800 Subject: [PATCH] =?UTF-8?q?art:=E8=AF=81=E4=B9=A6=E7=B1=BB=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=AF=BB=E5=8F=96=E4=BC=98=E5=8C=96=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binarywang/wxpay/config/WxPayConfig.java | 131 ++++++++---------- 1 file changed, 56 insertions(+), 75 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 f5cd91c6c3..097a0fa0c7 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 @@ -115,8 +115,6 @@ public class WxPayConfig { * apiV3 证书序列号值 */ private String certSerialNo; - - /** * 微信支付分serviceId */ @@ -128,8 +126,10 @@ public class WxPayConfig { private String payScoreNotifyUrl; private CloseableHttpClient apiV3HttpClient; - - + /** + * 私钥信息 + */ + private PrivateKey privateKey; /** * p12证书文件内容的字节数组. */ @@ -197,44 +197,7 @@ public SSLContext initSSLContext() throws WxPayException { if (StringUtils.isBlank(this.getKeyPath())) { throw new WxPayException("请确保证书文件地址keyPath已配置"); } - - final String prefix = "classpath:"; - String fileHasProblemMsg = String.format(PROBLEM_MSG, this.getKeyPath()); - String fileNotFoundMsg = String.format(NOT_FOUND_MSG, this.getKeyPath()); - if (this.getKeyPath().startsWith(prefix)) { - String path = RegExUtils.removeFirst(this.getKeyPath(), prefix); - if (!path.startsWith("/")) { - path = "/" + path; - } - try { - inputStream = ResourcesUtil.getResourceAsStream(path); - if (inputStream == null) { - throw new WxPayException(fileNotFoundMsg); - } - } catch (Exception e) { - throw new WxPayException(fileNotFoundMsg, e); - } - } else if (this.getKeyPath().startsWith("http://") || this.getKeyPath().startsWith("https://")) { - try { - inputStream = new URL(this.keyPath).openStream(); - if (inputStream == null) { - throw new WxPayException(fileNotFoundMsg); - } - } catch (IOException e) { - throw new WxPayException(fileNotFoundMsg, e); - } - } else { - try { - File file = new File(this.getKeyPath()); - if (!file.exists()) { - throw new WxPayException(fileNotFoundMsg); - } - - inputStream = new FileInputStream(file); - } catch (IOException e) { - throw new WxPayException(fileHasProblemMsg, e); - } - } + inputStream = this.loadConfigInputStream(this.getKeyPath()); } try { @@ -275,39 +238,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException { throw new WxPayException("请确保apiV3Key值已设置"); } - InputStream keyInputStream = null; - InputStream certInputStream = null; - final String prefix = "classpath:"; - if (privateKeyPath.startsWith(prefix)) { - String keypath = RegExUtils.removeFirst(privateKeyPath, prefix); - if (!keypath.startsWith("/")) { - keypath = "/" + keypath; - } - try { - keyInputStream = ResourcesUtil.getResourceAsStream(keypath); - if (keyInputStream == null) { - throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateKeyPath())); - } - } catch (Exception e) { - throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateKeyPath()), e); - } - } - - if (privateCertPath.startsWith(prefix)) { - String certpath = RegExUtils.removeFirst(privateCertPath, prefix); - if (!certpath.startsWith("/")) { - certpath = "/" + certpath; - } - try { - certInputStream = ResourcesUtil.getResourceAsStream(certpath); - if (certInputStream == null) { - throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateCertPath())); - } - } catch (Exception e) { - throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateCertPath()), e); - } - } - + InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath); + InputStream certInputStream = this.loadConfigInputStream(privateCertPath); try { PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); @@ -323,10 +255,59 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException { .build(); this.apiV3HttpClient = httpClient; this.verifier=verifier; + this.privateKey = merchantPrivateKey; return httpClient; } catch (Exception e) { throw new WxPayException("v3请求构造异常!", e); } } + + /** + * 从配置路径 加载配置 信息(支持 classpath、本地路径、网络url) + * @param configPath 配置路径 + * @return + * @throws WxPayException + */ + private InputStream loadConfigInputStream(String configPath) throws WxPayException { + InputStream inputStream; + final String prefix = "classpath:"; + String fileHasProblemMsg = String.format(PROBLEM_MSG, configPath); + String fileNotFoundMsg = String.format(NOT_FOUND_MSG, configPath); + if (configPath.startsWith(prefix)) { + String path = RegExUtils.removeFirst(configPath, prefix); + if (!path.startsWith("/")) { + path = "/" + path; + } + try { + inputStream = ResourcesUtil.getResourceAsStream(path); + if (inputStream == null) { + throw new WxPayException(fileNotFoundMsg); + } + } catch (Exception e) { + throw new WxPayException(fileNotFoundMsg, e); + } + } else if (configPath.startsWith("http://") || configPath.startsWith("https://")) { + try { + inputStream = new URL(configPath).openStream(); + if (inputStream == null) { + throw new WxPayException(fileNotFoundMsg); + } + } catch (IOException e) { + throw new WxPayException(fileNotFoundMsg, e); + } + } else { + try { + File file = new File(configPath); + if (!file.exists()) { + throw new WxPayException(fileNotFoundMsg); + } + + inputStream = new FileInputStream(file); + } catch (IOException e) { + throw new WxPayException(fileHasProblemMsg, e); + } + } + return inputStream; + } }