Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}