8000 #586 微信支付 WxPayConfig增加支持byte数组方式设置证书 · devandroid/weixin-java-tools@06c356b · GitHub
[go: up one dir, main page]

Skip to content

Commit 06c356b

Browse files
committed
binarywang#586 微信支付 WxPayConfig增加支持byte数组方式设置证书
1 parent 71f97c0 commit 06c356b

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.config;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.File;
45
import java.io.FileInputStream;
56
import java.io.IOException;
@@ -74,9 +75,14 @@ public class WxPayConfig {
7475
private String signType;
7576
private SSLContext sslContext;
7677
/**
77-
* 证书apiclient_cert.p12的文件的绝对路径.
78+
* p12证书文件的绝对路径或者以classpath:开头的类路径.
7879
*/
7980
private String keyPath;
81+
82+
/**
83+
* p12证书文件内容的字节数组.
84+
*/
85+
private byte[] keyContent;
8086
/**
8187
* 微信支付是否使用仿真测试环境.
8288
* 默认不使用
@@ -95,33 +101,37 @@ public SSLContext initSSLContext() throws WxPayException {
95101
throw new WxPayException("请确保商户号mchId已设置");
96102
}
97103

98-
if (StringUtils.isBlank(this.getKeyPath())) {
99-
throw new WxPayException("请确保证书文件地址keyPath已配置");
100-
}
101-
102104
InputStream inputStream;
103-
final String prefix = "classpath:";
104-
String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!";
105-
String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!";
106-
if (this.getKeyPath().startsWith(prefix)) {
107-
String path = StringUtils.removeFirst(this.getKeyPath(), prefix);
108-
if (!path.startsWith("/")) {
109-
path = "/" + path;
110-
}
111-
inputStream = WxPayConfig.class.getResourceAsStream(path);
112-
if (inputStream == null) {
113-
throw new WxPayException(fileNotFoundMsg);
114-
}
105+
if (this.keyContent != null) {
106+
inputStream = new ByteArrayInputStream(this.keyContent);
115107
} else {
116-
try {
117-
File file = new File(this.getKeyPath());
118-
if (!file.exists()) {
108+
if (StringUtils.isBlank(this.getKeyPath())) {
109+
throw new WxPayException("请确保证书文件地址keyPath已配置");
110+
}
111+
112+
final String prefix = "classpath:";
113+
String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!";
114+
String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!";
115+
if (this.getKeyPath().startsWith(prefix)) {
116+
String path = StringUtils.removeFirst(this.getKeyPath(), prefix);
117+
if (!path.startsWith("/")) {
118+
path = "/" + path;
119+
}
120+
inputStream = WxPayConfig.class.getResourceAsStream(path);
121+
if (inputStream == null) {
119122
throw new WxPayException(fileNotFoundMsg);
120123
}
124+
} else {
125+
try {
126+
File file = new File(this.getKeyPath());
127+
if (!file.exists()) {
128+
throw new WxPayException(fileNotFoundMsg);
129+
}
121130

122-
inputStream = new FileInputStream(file);
123-
} catch (IOException e) {
124-
throw new WxPayException(fileHasProblemMsg, e);
131+
inputStream = new FileInputStream(file);
132+
} catch (IOException e) {
133+
throw new WxPayException(fileHasProblemMsg, e);
134+
}
125135
}
126136
}
127137

@@ -132,7 +142,7 @@ public SSLContext initSSLContext() throws WxPayException {
132142
this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
133143
return this.sslContext;
134144
} catch (Exception e) {
135-
throw new WxPayException(fileHasProblemMsg, e);
145+
throw new WxPayException("证书文件有问题,请核实!", e);
136146
} finally {
137147
IOUtils.closeQuietly(inputStream);
138148
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.github.binarywang.wxpay.service.impl;
22

3-
import com.github.binarywang.wxpay.bean.WxPayApiData;
4-
import com.github.binarywang.wxpay.exception.WxPayException;
5-
import jodd.util.Base64;
3+
import java.io.UnsupportedEncodingException;
4+
import java.nio.charset.StandardCharsets;
5+
import javax.net.ssl.SSLContext;
6+
67
import org.apache.commons.lang3.StringUtils;
78
import org.apache.http.auth.AuthScope;
89
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -19,9 +20,9 @@
1920
import org.apache.http.impl.client.HttpClients;
2021
import org.apache.http.util.EntityUtils;
2122

22-
import javax.net.ssl.SSLContext;
23-
import java.io.UnsupportedEncodingException;
24-
import java.nio.charset.StandardCharsets;
23+
import com.github.binarywang.wxpay.bean.WxPayApiData;
24+
import com.github.binarywang.wxpay.exception.WxPayException;
25+
import jodd.util.Base64;
2526

2627
/**
2728
* <pre>
@@ -37,8 +38,8 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
3738
try {
3839
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey);
3940
HttpPost httpPost = this.createHttpPost(url, requestStr);
40-
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
41-
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
41+
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
42+
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
4243
final byte[] bytes = EntityUtils.toByteArray(response.getEntity());
4344
final String responseData = Base64.encodeToString(bytes);
4445
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData);
@@ -60,8 +61,8 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
6061
try {
6162
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
6263
HttpPost httpPost = this.createHttpPost(url, requestStr);
63-
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
64-
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
64+
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
65+
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
6566
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
6667
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
6768
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
@@ -90,7 +91,7 @@ private StringEntity createEntry(String requestStr) {
9091
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
9192
HttpClientBuilder httpClientBuilder = HttpClients.custom();
9293
if (useKey) {
93-
this.setKey(httpClientBuilder);
94+
this.initSSLContext(httpClientBuilder);
9495
}
9596

9697
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost())
@@ -118,15 +119,15 @@ private HttpPost createHttpPost(String url, String requestStr) {
118119
return httpPost;
119120
}
120121

121-
private void setKey(HttpClientBuilder httpClientBuilder) throws WxPayException {
122+
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
122123
SSLContext sslContext = this.getConfig().getSslContext();
123124
if (null == sslContext) {
124125
sslContext = this.getConfig().initSSLContext();
125126
}
126127

127-
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
128+
SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
128129
new String[]{"TLSv1"}, null, new DefaultHostnameVerifier());
129-
httpClientBuilder.setSSLSocketFactory(sslsf);
130+
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
130131
}
131132

132133
}

0 commit comments

Comments
 (0)
0