Skip to content

Commit

Permalink
🎨 #1716 微信支付证书配置读取代码优化,支持本地路径和网络路径
Browse files Browse the repository at this point in the history
Co-authored-by: 曾浩 <[email protected]>
  • Loading branch information
cloudX2019 and 曾浩 authored Aug 17, 2020
1 parent 43633aa commit a278383
Showing 1 changed file with 56 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public class WxPayConfig {
* apiV3 证书序列号值
*/
private String certSerialNo;


/**
* 微信支付分serviceId
*/
Expand All @@ -128,8 +126,10 @@ public class WxPayConfig {
private String payScoreNotifyUrl;

private CloseableHttpClient apiV3HttpClient;


/**
* 私钥信息
*/
private PrivateKey privateKey;
/**
* p12证书文件内容的字节数组.
*/
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
}

0 comments on commit a278383

Please sign in to comment.