art:证书类配置读取优化调整

This commit is contained in:
曾浩 2020-08-17 13:32:42 +08:00
parent 43633aacc2
commit 1812b233bb

View File

@ -115,8 +115,6 @@ public class WxPayConfig {
* apiV3 证书序列号值
*/
private String certSerialNo;
/**
* 微信支付分serviceId
*/
@ -128,8 +126,10 @@ public class WxPayConfig {
private String payScoreNotifyUrl;
private CloseableHttpClient apiV3HttpClient;
/**
* 私钥信息
*/
private PrivateKey privateKey;
/**
* p12证书文件内容的字节数组.
*/
@ -197,44 +197,7 @@ public class WxPayConfig {
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 {
@ -275,39 +238,8 @@ public class WxPayConfig {
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);
@ -323,10 +255,59 @@ public class WxPayConfig {
.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;
}
}