Skip to content

Commit 3534640

Browse files
authored
去除getLatestCertificate方法 (#101)
1 parent fbb8125 commit 3534640

File tree

6 files changed

+21
-35
lines changed

6 files changed

+21
-35
lines changed

UPGRADING.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 升级指南
22
## 从 0.3.0 升级至 0.4.0
3-
版本`0.4.0`提供了支持多商户号的[定时更新平台证书功能](#定时更新平台证书功能),不兼容版本`0.3.0`。若你使用了`ScheduledUpdateCertificatesVerifier`,请使用`CertificatesManager`替换:
3+
版本`0.4.0`提供了支持多商户号的[定时更新平台证书功能](README.md#定时更新平台证书功能),不兼容版本`0.3.0`。推荐升级方式如下:
4+
- 若你使用了`ScheduledUpdateCertificatesVerifier`,请使用`CertificatesManager`替换:
45
```diff
56
-verifier = new ScheduledUpdateCertificatesVerifier(
67
- new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
@@ -13,3 +14,4 @@
1314
+// 从证书管理器中获取verifier
1415
+verifier = certificatesManager.getVerifier(mchId);
1516
```
17+
- 若你使用了`getLatestCertificate`方法,请使用`getValidCertificate`方法替换。

src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/AutoUpdateCertificatesVerifier.java

-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ public X509Certificate getValidCertificate() {
9898
return verifier.getValidCertificate();
9999
}
100100

101-
@Override
102-
public X509Certificate getLatestCertificate() {
103-
return verifier.getLatestCertificate();
104-
}
105-
106101
protected void autoUpdateCert() throws IOException, GeneralSecurityException {
107102
try (CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
108103
.withCredentials(credentials)

src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/CertificatesVerifier.java

+6-13
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,19 @@ public boolean verify(String serialNumber, byte[] message, String signature) {
6262

6363
@Override
6464
public X509Certificate getValidCertificate() {
65-
for (X509Certificate x509Cert : certificates.values()) {
66-
try {
67-
x509Cert.checkValidity();
68-
return x509Cert;
69-
} catch (CertificateExpiredException | CertificateNotYetValidException ignored) {
70-
}
71-
}
72-
throw new NoSuchElementException("没有有效的微信支付平台证书");
73-
}
74-
75-
@Override
76-
public X509Certificate getLatestCertificate() {
7765
X509Certificate latestCert = null;
7866
for (X509Certificate x509Cert : certificates.values()) {
7967
// 若latestCert为空或x509Cert的证书有效开始时间在latestCert之后,则更新latestCert
8068
if (latestCert == null || x509Cert.getNotBefore().after(latestCert.getNotBefore())) {
8169
latestCert = x509Cert;
8270
}
8371
}
84-
return latestCert;
72+
try {
73+
latestCert.checkValidity();
74+
return latestCert;
75+
} catch (CertificateExpiredException | CertificateNotYetValidException e) {
76+
throw new NoSuchElementException("没有有效的微信支付平台证书");
77+
}
8578
}
8679
}
8780

src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/Verifier.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ public interface Verifier {
1010
boolean verify(String serialNumber, byte[] message, String signature);
1111

1212
/**
13-
* 该方法已废弃,请使用getLatestCertificate代替
13+
* 获取合法的平台证书
1414
*
1515
* @return 合法证书
1616
*/
17-
@Deprecated
1817
X509Certificate getValidCertificate();
19-
20-
X509Certificate getLatestCertificate();
21-
2218
}

src/main/java/com/wechat/pay/contrib/apache/httpclient/cert/CertificatesManager.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,11 @@ public boolean verify(String serialNumber, byte[] message, String signature) {
100100

101101
@Override
102102
public X509Certificate getValidCertificate() {
103-
return null;
104-
}
105-
106-
@Override
107-
public X509Certificate getLatestCertificate() {
108103
X509Certificate certificate;
109104
try {
110105
certificate = CertificatesManager.this.getLatestCertificate(merchantId);
111106
} catch (NotFoundException e) {
112-
throw new NoSuchElementException("没有最新的平台证书,merchantId:");
107+
throw new NoSuchElementException("没有有效的微信支付平台证书");
113108
}
114109
return certificate;
115110
}
@@ -176,7 +171,7 @@ public void stop() {
176171
}
177172
}
178173

179-
public X509Certificate getLatestCertificate(String merchantId)
174+
private X509Certificate getLatestCertificate(String merchantId)
180175
throws NotFoundException {
181176
if (merchantId == null || merchantId.isEmpty()) {
182177
throw new IllegalArgumentException("merchantId为空");
@@ -205,7 +200,8 @@ public X509Certificate getLatestCertificate(String merchantId)
205200
* 获取商户号为merchantId的验签器
206201
*
207202
* @param merchantId 商户号
208-
* @return verifier
203+
* @return 验签器
204+
* @throws NotFoundException merchantId/merchantCertificates/apiV3Key/credentials为空
209205
*/
210206
public Verifier getVerifier(String merchantId) throws NotFoundException {
211207
// 若商户信息不存在,返回错误

src/test/java/com/wechat/pay/contrib/apache/httpclient/RsaCryptoTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.junit.Assert.assertTrue;
99

1010
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
11+
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
1112
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
1213
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
1314
import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
@@ -37,7 +38,7 @@ public class RsaCryptoTest {
3738

3839
private CloseableHttpClient httpClient;
3940
private CertificatesManager certificatesManager;
40-
41+
private Verifier verifier;
4142

4243
@Before
4344
public void setup() throws Exception {
@@ -48,6 +49,7 @@ public void setup() throws Exception {
4849
certificatesManager.putMerchant(mchId, new WechatPay2Credentials(mchId,
4950
new PrivateKeySigner(mchSerialNo, merchantPrivateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8));
5051
// 从证书管理器中获取verifier
52+
verifier = certificatesManager.getVerifier(mchId);
5153
httpClient = WechatPayHttpClientBuilder.create()
5254
.withMerchant(mchId, mchSerialNo, merchantPrivateKey)
5355
.withValidator(new WechatPay2Validator(certificatesManager.getVerifier(mchId)))
@@ -62,7 +64,8 @@ public void after() throws IOException {
6264
@Test
6365
public void encryptTest() throws Exception {
6466
String text = "helloworld";
65-
String ciphertext = RsaCryptoUtil.encryptOAEP(text, certificatesManager.getLatestCertificate(mchId));
67+
String ciphertext = RsaCryptoUtil
68+
.encryptOAEP(text, verifier.getValidCertificate());
6669
System.out.println("ciphertext: " + ciphertext);
6770
}
6871

@@ -71,7 +74,8 @@ public void postEncryptDataTest() throws Exception {
7174
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/smartguide/guides");
7275

7376
String text = "helloworld";
74-
String ciphertext = RsaCryptoUtil.encryptOAEP(text, certificatesManager.getLatestCertificate(mchId));
77+
String ciphertext = RsaCryptoUtil
78+
.encryptOAEP(text, verifier.getValidCertificate());
7579

7680
String data = "{\n"
7781
+ " \"store_id\" : 1234,\n"

0 commit comments

Comments
 (0)