🎨 优化WxPayConfig,支持配置文件为字节数组

This commit is contained in:
S 2021-12-02 18:22:16 +08:00 committed by GitHub
parent 40ba792acc
commit a254d1ce34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -108,6 +108,16 @@ public class WxPayConfig {
*/ */
private String privateCertPath; private String privateCertPath;
/**
* apiclient_key.pem证书文件内容的字节数组.
*/
private byte[] privateKeyContent;
/**
* apiclient_cert.pem证书文件内容的字节数组.
*/
private byte[] privateCertContent;
/** /**
* apiV3 秘钥值. * apiV3 秘钥值.
*/ */
@ -205,15 +215,7 @@ public class WxPayConfig {
throw new WxPayException("请确保商户号mchId已设置"); throw new WxPayException("请确保商户号mchId已设置");
} }
InputStream inputStream; InputStream inputStream = this.loadConfigInputStream(this.getKeyPath(), this.keyContent, "p12证书");
if (this.keyContent != null) {
inputStream = new ByteArrayInputStream(this.keyContent);
} else {
if (StringUtils.isBlank(this.getKeyPath())) {
throw new WxPayException("请确保证书文件地址keyPath已配置");
}
inputStream = this.loadConfigInputStream(this.getKeyPath());
}
try { try {
KeyStore keystore = KeyStore.getInstance("PKCS12"); KeyStore keystore = KeyStore.getInstance("PKCS12");
@ -240,21 +242,12 @@ public class WxPayConfig {
val privateCertPath = this.getPrivateCertPath(); val privateCertPath = this.getPrivateCertPath();
val serialNo = this.getCertSerialNo(); val serialNo = this.getCertSerialNo();
val apiV3Key = this.getApiV3Key(); val apiV3Key = this.getApiV3Key();
if (StringUtils.isBlank(privateKeyPath)) {
throw new WxPayException("请确保privateKeyPath已设置");
}
if (StringUtils.isBlank(privateCertPath)) {
throw new WxPayException("请确保privateCertPath已设置");
}
// if (StringUtils.isBlank(certSerialNo)) {
// throw new WxPayException("请确保certSerialNo证书序列号已设置");
// }
if (StringUtils.isBlank(apiV3Key)) { if (StringUtils.isBlank(apiV3Key)) {
throw new WxPayException("请确保apiV3Key值已设置"); throw new WxPayException("请确保apiV3Key值已设置");
} }
InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath); InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath, this.privateKeyContent, "privateKeyPath");
InputStream certInputStream = this.loadConfigInputStream(privateCertPath); InputStream certInputStream = this.loadConfigInputStream(privateCertPath, this.privateCertContent, "privateCertPath");
try { try {
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
X509Certificate certificate = PemUtils.loadCertificate(certInputStream); X509Certificate certificate = PemUtils.loadCertificate(certInputStream);
@ -281,6 +274,22 @@ public class WxPayConfig {
} }
} }
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
InputStream inputStream;
if (configContent != null) {
inputStream = new ByteArrayInputStream(configContent);
} else {
if (StringUtils.isBlank(configPath)) {
throw new WxPayException("请确保证书文件地址【" + fileName + "】或者内容已配置");
}
inputStream = this.loadConfigInputStream(configPath);
}
return inputStream;
}
/** /**
* 从配置路径 加载配置 信息支持 classpath本地路径网络url * 从配置路径 加载配置 信息支持 classpath本地路径网络url
* @param configPath 配置路径 * @param configPath 配置路径