Skip to content

Commit c7ffff0

Browse files
committed
#251 微信支付证书文件路径配置支持classpath开头的地址
1 parent ec4bf26 commit c7ffff0

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

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

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

3+
import com.github.binarywang.wxpay.exception.WxPayException;
4+
import org.apache.commons.io.IOUtils;
35
import org.apache.commons.lang3.StringUtils;
46
import org.apache.http.ssl.SSLContexts;
57

68
import javax.net.ssl.SSLContext;
79
import java.io.File;
810
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
913
import java.security.KeyStore;
1014

1115
/**
@@ -143,7 +147,7 @@ public void setUseSandboxEnv(boolean useSandboxEnv) {
143147
this.useSandboxEnv = useSandboxEnv;
144148
}
145149

146-
public SSLContext initSSLContext() {
150+
public SSLContext initSSLContext() throws WxPayException {
147151
if (StringUtils.isBlank(mchId)) {
148152
throw new IllegalArgumentException("请确保商户号mchId已设置");
149153
}
@@ -152,20 +156,33 @@ public SSLContext initSSLContext() {
152156
throw new IllegalArgumentException("请确保证书文件地址keyPath已配置");
153157
}
154158

155-
File file = new File(this.keyPath);
156-
if (!file.exists()) {
157-
throw new RuntimeException("证书文件【" + file.getPath() + "】不存在!");
159+
InputStream inputStream;
160+
final String prefix = "classpath:";
161+
if (this.keyPath.startsWith(prefix)) {
162+
inputStream = WxPayConfig.class.getResourceAsStream(this.keyPath.replace(prefix, ""));
163+
} else {
164+
try {
165+
File file = new File(this.keyPath);
166+
if (!file.exists()) {
167+
throw new WxPayException("证书文件【" + file.getPath() + "】不存在!");
168+
}
169+
170+
inputStream = new FileInputStream(file);
171+
} catch (IOException e) {
172+
throw new WxPayException("证书文件有问题,请核实!", e);
173+
}
158174
}
159175

160176
try {
161-
FileInputStream inputStream = new FileInputStream(file);
162177
KeyStore keystore = KeyStore.getInstance("PKCS12");
163178
char[] partnerId2charArray = mchId.toCharArray();
164179
keystore.load(inputStream, partnerId2charArray);
165180
this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
166181
return this.sslContext;
167182
} catch (Exception e) {
168-
throw new RuntimeException("证书文件有问题,请核实!", e);
183+
throw new WxPayException("证书文件有问题,请核实!", e);
184+
} finally {
185+
IOUtils.closeQuietly(inputStream);
169186
}
170187
}
171188
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/exception/WxPayException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public WxPayException(String customErrorMsg) {
4545
this.customErrorMsg = customErrorMsg;
4646
}
4747

48+
public WxPayException(String customErrorMsg, Throwable tr) {
49+
super(customErrorMsg, tr);
50+
this.customErrorMsg = customErrorMsg;
51+
}
52+
4853
private WxPayException(Builder builder) {
4954
super(builder.buildErrorMsg());
5055
returnCode = builder.returnCode;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ private String postWithKey(String url, String requestStr) throws WxPayException
497497

498498
String responseString = this.getResponseString(request.send());
499499

500-
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString);
500+
this.log.info("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString);
501501
return responseString;
502502
} catch (Exception e) {
503503
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.binarywang.wxpay.config;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static org.testng.Assert.*;
6+
7+
/**
8+
* <pre>
9+
* Created by BinaryWang on 2017/6/18.
10+
* </pre>
11+
*
12+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
13+
*/
14+
public class WxPayConfigTest {
15+
private WxPayConfig payConfig = new WxPayConfig();
16+
17+
@Test
18+
public void testInitSSLContext() throws Exception {
19+
payConfig.setMchId("123");
20+
payConfig.setKeyPath("classpath:/abc.p12");
21+
payConfig.initSSLContext();
22+
}
23+
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.p12

0 commit comments

Comments
 (0)