From 06c356bdcc94179e3f189c6ae073509cf87506f3 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 13 May 2018 18:39:42 +0800 Subject: [PATCH] =?UTF-8?q?#586=20=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98=20W?= =?UTF-8?q?xPayConfig=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81byte=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=96=B9=E5=BC=8F=E8=AE=BE=E7=BD=AE=E8=AF=81=E4=B9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binarywang/wxpay/config/WxPayConfig.java | 58 +++++++++++-------- .../impl/WxPayServiceApacheHttpImpl.java | 29 +++++----- 2 files changed, 49 insertions(+), 38 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 a43724cf..cf37fa92 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 @@ -1,5 +1,6 @@ package com.github.binarywang.wxpay.config; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -74,9 +75,14 @@ public class WxPayConfig { private String signType; private SSLContext sslContext; /** - * 证书apiclient_cert.p12的文件的绝对路径. + * p12证书文件的绝对路径或者以classpath:开头的类路径. */ private String keyPath; + + /** + * p12证书文件内容的字节数组. + */ + private byte[] keyContent; /** * 微信支付是否使用仿真测试环境. * 默认不使用 @@ -95,33 +101,37 @@ public class WxPayConfig { throw new WxPayException("请确保商户号mchId已设置"); } - if (StringUtils.isBlank(this.getKeyPath())) { - throw new WxPayException("请确保证书文件地址keyPath已配置"); - } - InputStream inputStream; - final String prefix = "classpath:"; - String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!"; - String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!"; - if (this.getKeyPath().startsWith(prefix)) { - String path = StringUtils.removeFirst(this.getKeyPath(), prefix); - if (!path.startsWith("/")) { - path = "/" + path; - } - inputStream = WxPayConfig.class.getResourceAsStream(path); - if (inputStream == null) { - throw new WxPayException(fileNotFoundMsg); - } + if (this.keyContent != null) { + inputStream = new ByteArrayInputStream(this.keyContent); } else { - try { - File file = new File(this.getKeyPath()); - if (!file.exists()) { + if (StringUtils.isBlank(this.getKeyPath())) { + throw new WxPayException("请确保证书文件地址keyPath已配置"); + } + + final String prefix = "classpath:"; + String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!"; + String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!"; + if (this.getKeyPath().startsWith(prefix)) { + String path = StringUtils.removeFirst(this.getKeyPath(), prefix); + if (!path.startsWith("/")) { + path = "/" + path; + } + inputStream = WxPayConfig.class.getResourceAsStream(path); + if (inputStream == null) { throw new WxPayException(fileNotFoundMsg); } + } 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 = new FileInputStream(file); + } catch (IOException e) { + throw new WxPayException(fileHasProblemMsg, e); + } } } @@ -132,7 +142,7 @@ public class WxPayConfig { this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build(); return this.sslContext; } catch (Exception e) { - throw new WxPayException(fileHasProblemMsg, e); + throw new WxPayException("证书文件有问题,请核实!", e); } finally { IOUtils.closeQuietly(inputStream); } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index 1644d260..a6cb5620 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -1,8 +1,9 @@ package com.github.binarywang.wxpay.service.impl; -import com.github.binarywang.wxpay.bean.WxPayApiData; -import com.github.binarywang.wxpay.exception.WxPayException; -import jodd.util.Base64; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import javax.net.ssl.SSLContext; + import org.apache.commons.lang3.StringUtils; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; @@ -19,9 +20,9 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; -import javax.net.ssl.SSLContext; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; +import com.github.binarywang.wxpay.bean.WxPayApiData; +import com.github.binarywang.wxpay.exception.WxPayException; +import jodd.util.Base64; /** *
@@ -37,8 +38,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
     try {
       HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey);
       HttpPost httpPost = this.createHttpPost(url, requestStr);
-      try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
-        try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
+      try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
+        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
           final byte[] bytes = EntityUtils.toByteArray(response.getEntity());
           final String responseData = Base64.encodeToString(bytes);
           this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData);
@@ -60,8 +61,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
     try {
       HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
       HttpPost httpPost = this.createHttpPost(url, requestStr);
-      try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
-        try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
+      try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
+        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
           String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
           this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
           wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
@@ -90,7 +91,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
   private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
     HttpClientBuilder httpClientBuilder = HttpClients.custom();
     if (useKey) {
-      this.setKey(httpClientBuilder);
+      this.initSSLContext(httpClientBuilder);
     }
 
     if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost())
@@ -118,15 +119,15 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
     return httpPost;
   }
 
-  private void setKey(HttpClientBuilder httpClientBuilder) throws WxPayException {
+  private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
     SSLContext sslContext = this.getConfig().getSslContext();
     if (null == sslContext) {
       sslContext = this.getConfig().initSSLContext();
     }
 
-    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
+    SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
       new String[]{"TLSv1"}, null, new DefaultHostnameVerifier());
-    httpClientBuilder.setSSLSocketFactory(sslsf);
+    httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
   }
 
 }