mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-27 20:14:52 +08:00
🎨 优化部分代码
This commit is contained in:
@ -10,6 +10,7 @@ import lombok.Data;
|
|||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.RegExUtils;
|
import org.apache.commons.lang3.RegExUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
@ -32,6 +33,7 @@ import java.util.Optional;
|
|||||||
* @author Binary Wang (<a href="https://github.com/binarywang">...</a>)
|
* @author Binary Wang (<a href="https://github.com/binarywang">...</a>)
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@Slf4j
|
||||||
@ToString(exclude = "verifier")
|
@ToString(exclude = "verifier")
|
||||||
@EqualsAndHashCode(exclude = "verifier")
|
@EqualsAndHashCode(exclude = "verifier")
|
||||||
public class WxPayConfig {
|
public class WxPayConfig {
|
||||||
@ -253,7 +255,7 @@ public class WxPayConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化api v3请求头 自动签名验签
|
* 初始化api v3请求头 自动签名验签
|
||||||
* 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
|
* 方法参照 <a href="https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient">微信支付官方api项目</a>
|
||||||
*
|
*
|
||||||
* @return org.apache.http.impl.client.CloseableHttpClient
|
* @return org.apache.http.impl.client.CloseableHttpClient
|
||||||
* @author doger.wang
|
* @author doger.wang
|
||||||
@ -397,8 +399,8 @@ public class WxPayConfig {
|
|||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
throw new WxPayException(fileNotFoundMsg);
|
throw new WxPayException(fileNotFoundMsg);
|
||||||
}
|
}
|
||||||
|
//使用Files.newInputStream打开公私钥文件,会存在无法释放句柄的问题
|
||||||
// return Files.newInputStream(file.toPath());
|
//return Files.newInputStream(file.toPath());
|
||||||
return new FileInputStream(file);
|
return new FileInputStream(file);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new WxPayException(fileHasProblemMsg, e);
|
throw new WxPayException(fileHasProblemMsg, e);
|
||||||
@ -408,36 +410,30 @@ public class WxPayConfig {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 分解p12证书文件
|
* 分解p12证书文件
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private Object[] p12ToPem() {
|
private Object[] p12ToPem() {
|
||||||
String key = getMchId();
|
String key = getMchId();
|
||||||
if (StringUtils.isBlank(key)) {
|
if (StringUtils.isBlank(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分解p12证书文件
|
// 分解p12证书文件
|
||||||
PrivateKey privateKey = null;
|
|
||||||
X509Certificate x509Certificate = null;
|
|
||||||
try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(),
|
try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(),
|
||||||
this.keyContent, "p12证书");){
|
this.keyContent, "p12证书");) {
|
||||||
if (inputStream == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
||||||
keyStore.load(inputStream, key.toCharArray());
|
keyStore.load(inputStream, key.toCharArray());
|
||||||
|
|
||||||
String alias = keyStore.aliases().nextElement();
|
String alias = keyStore.aliases().nextElement();
|
||||||
privateKey = (PrivateKey) keyStore.getKey(alias, key.toCharArray());
|
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, key.toCharArray());
|
||||||
|
|
||||||
Certificate certificate = keyStore.getCertificate(alias);
|
Certificate certificate = keyStore.getCertificate(alias);
|
||||||
x509Certificate = (X509Certificate) certificate;
|
X509Certificate x509Certificate = (X509Certificate) certificate;
|
||||||
return new Object[]{privateKey, x509Certificate};
|
return new Object[]{privateKey, x509Certificate};
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
log.error("加载证书时发生异常", e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user