🎨 #1716 微信支付证书配置读取代码优化,支持本地路径和网络路径
Co-authored-by: 曾浩 <epdcgsi@dingtalk.com>
This commit is contained in:
@@ -115,8 +115,6 @@ public class WxPayConfig {
|
|||||||
* apiV3 证书序列号值
|
* apiV3 证书序列号值
|
||||||
*/
|
*/
|
||||||
private String certSerialNo;
|
private String certSerialNo;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信支付分serviceId
|
* 微信支付分serviceId
|
||||||
*/
|
*/
|
||||||
@@ -128,8 +126,10 @@ public class WxPayConfig {
|
|||||||
private String payScoreNotifyUrl;
|
private String payScoreNotifyUrl;
|
||||||
|
|
||||||
private CloseableHttpClient apiV3HttpClient;
|
private CloseableHttpClient apiV3HttpClient;
|
||||||
|
/**
|
||||||
|
* 私钥信息
|
||||||
|
*/
|
||||||
|
private PrivateKey privateKey;
|
||||||
/**
|
/**
|
||||||
* p12证书文件内容的字节数组.
|
* p12证书文件内容的字节数组.
|
||||||
*/
|
*/
|
||||||
@@ -197,44 +197,7 @@ public class WxPayConfig {
|
|||||||
if (StringUtils.isBlank(this.getKeyPath())) {
|
if (StringUtils.isBlank(this.getKeyPath())) {
|
||||||
throw new WxPayException("请确保证书文件地址keyPath已配置");
|
throw new WxPayException("请确保证书文件地址keyPath已配置");
|
||||||
}
|
}
|
||||||
|
inputStream = this.loadConfigInputStream(this.getKeyPath());
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -275,39 +238,8 @@ public class WxPayConfig {
|
|||||||
throw new WxPayException("请确保apiV3Key值已设置");
|
throw new WxPayException("请确保apiV3Key值已设置");
|
||||||
}
|
}
|
||||||
|
|
||||||
InputStream keyInputStream = null;
|
InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath);
|
||||||
InputStream certInputStream = null;
|
InputStream certInputStream = this.loadConfigInputStream(privateCertPath);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
|
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
|
||||||
|
|
||||||
@@ -323,10 +255,59 @@ public class WxPayConfig {
|
|||||||
.build();
|
.build();
|
||||||
this.apiV3HttpClient = httpClient;
|
this.apiV3HttpClient = httpClient;
|
||||||
this.verifier=verifier;
|
this.verifier=verifier;
|
||||||
|
this.privateKey = merchantPrivateKey;
|
||||||
|
|
||||||
return httpClient;
|
return httpClient;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new WxPayException("v3请求构造异常!", 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user