Skip to content

Commit a278383

Browse files
cloudX2019曾浩
andauthored
🎨 #1716 微信支付证书配置读取代码优化,支持本地路径和网络路径
Co-authored-by: 曾浩 <[email protected]>
1 parent 43633aa commit a278383

File tree

1 file changed

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

1 file changed

+56
-75
lines changed

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

Lines changed: 56 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ public class WxPayConfig {
115115
* apiV3 证书序列号值
116116
*/
117117
private String certSerialNo;
118-
119-
120118
/**
121119
* 微信支付分serviceId
122120
*/
@@ -128,8 +126,10 @@ public class WxPayConfig {
128126
private String payScoreNotifyUrl;
129127

130128
private CloseableHttpClient apiV3HttpClient;
131-
132-
129+
/**
130+
* 私钥信息
131+
*/
132+
private PrivateKey privateKey;
133133
/**
134134
* p12证书文件内容的字节数组.
135135
*/
@@ -197,44 +197,7 @@ public SSLContext initSSLContext() throws WxPayException {
197197
if (StringUtils.isBlank(this.getKeyPath())) {
198198
throw new WxPayException("请确保证书文件地址keyPath已配置");
199199
}
200-
201-
final String prefix = "classpath:";
202-
String fileHasProblemMsg = String.format(PROBLEM_MSG, this.getKeyPath());
203-
String fileNotFoundMsg = String.format(NOT_FOUND_MSG, this.getKeyPath());
204-
if (this.getKeyPath().startsWith(prefix)) {
205-
String path = RegExUtils.removeFirst(this.getKeyPath(), prefix);
206-
if (!path.startsWith("/")) {
207-
path = "/" + path;
208-
}
209-
try {
210-
inputStream = ResourcesUtil.getResourceAsStream(path);
211-
if (inputStream == null) {
212-
throw new WxPayException(fileNotFoundMsg);
213-
}
214-
} catch (Exception e) {
215-
throw new WxPayException(fileNotFoundMsg, e);
216-
}
217-
} else if (this.getKeyPath().startsWith("http://") || this.getKeyPath().startsWith("https://")) {
218-
try {
219-
inputStream = new URL(this.keyPath).openStream();
220-
if (inputStream == null) {
221-
throw new WxPayException(fileNotFoundMsg);
222-
}
223-
} catch (IOException e) {
224-
throw new WxPayException(fileNotFoundMsg, e);
225-
}
226-
} else {
227-
try {
228-
File file = new File(this.getKeyPath());
229-
if (!file.exists()) {
230-
throw new WxPayException(fileNotFoundMsg);
231-
}
232-
233-
inputStream = new FileInputStream(file);
234-
} catch (IOException e) {
235-
throw new WxPayException(fileHasProblemMsg, e);
236-
}
237-
}
200+
inputStream = this.loadConfigInputStream(this.getKeyPath());
238201
}
239202

240203
try {
@@ -275,39 +238,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
275238
throw new WxPayException("请确保apiV3Key值已设置");
276239
}
277240

278-
InputStream keyInputStream = null;
279-
InputStream certInputStream = null;
280-
final String prefix = "classpath:";
281-
if (privateKeyPath.startsWith(prefix)) {
282-
String keypath = RegExUtils.removeFirst(privateKeyPath, prefix);
283-
if (!keypath.startsWith("/")) {
284-
keypath = "/" + keypath;
285-
}
286-
try {
287-
keyInputStream = ResourcesUtil.getResourceAsStream(keypath);
288-
if (keyInputStream == null) {
289-
throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateKeyPath()));
290-
}
291-
} catch (Exception e) {
292-
throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateKeyPath()), e);
293-
}
294-
}
295-
296-
if (privateCertPath.startsWith(prefix)) {
297-
String certpath = RegExUtils.removeFirst(privateCertPath, prefix);
298-
if (!certpath.startsWith("/")) {
299-
certpath = "/" + certpath;
300-
}
301-
try {
302-
certInputStream = ResourcesUtil.getResourceAsStream(certpath);
303-
if (certInputStream == null) {
304-
throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateCertPath()));
305-
}
306-
} catch (Exception e) {
307-
throw new WxPayException(String.format(NOT_FOUND_MSG, this.getPrivateCertPath()), e);
308-
}
309-
}
310-
241+
InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath);
242+
InputStream certInputStream = this.loadConfigInputStream(privateCertPath);
311243
try {
312244
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
313245

@@ -323,10 +255,59 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
323255
.build();
324256
this.apiV3HttpClient = httpClient;
325257
this.verifier=verifier;
258+
this.privateKey = merchantPrivateKey;
326259

327260
return httpClient;
328261
} catch (Exception e) {
329262
throw new WxPayException("v3请求构造异常!", e);
330263
}
331264
}
265+
266+
/**
267+
* 从配置路径 加载配置 信息(支持 classpath、本地路径、网络url)
268+
* @param configPath 配置路径
269+
* @return
270+
* @throws WxPayException
271+
*/
272+
private InputStream loadConfigInputStream(String configPath) throws WxPayException {
273+
InputStream inputStream;
274+
final String prefix = "classpath:";
275+
String fileHasProblemMsg = String.format(PROBLEM_MSG, configPath);
276+
String fileNotFoundMsg = String.format(NOT_FOUND_MSG, configPath);
277+
if (configPath.startsWith(prefix)) {
278+
String path = RegExUtils.removeFirst(configPath, prefix);
279+
if (!path.startsWith("/")) {
280+
path = "/" + path;
281+
}
282+
try {
283+
inputStream = ResourcesUtil.getResourceAsStream(path);
284+
if (inputStream == null) {
285+
throw new WxPayException(fileNotFoundMsg);
286+
}
287+
} catch (Exception e) {
288+
throw new WxPayException(fileNotFoundMsg, e);
289+
}
290+
} else if (configPath.startsWith("http://") || configPath.startsWith("https://")) {
291+
try {
292+
inputStream = new URL(configPath).openStream();
293+
if (inputStream == null) {
294+
throw new WxPayException(fileNotFoundMsg);
295+
}
296+
} catch (IOException e) {
297+
throw new WxPayException(fileNotFoundMsg, e);
298+
}
299+
} else {
300+
try {
301+
File file = new File(configPath);
302+
if (!file.exists()) {
303+
throw new WxPayException(fileNotFoundMsg);
304+
}
305+
306+
inputStream = new FileInputStream(file);
307+
} catch (IOException e) {
308+
throw new WxPayException(fileHasProblemMsg, e);
309+
}
310+
}
311+
return inputStream;
312+
}
332313
}

0 commit comments

Comments
 (0)