Skip to content

Commit e2df7ab

Browse files
时间相关操作优化,提高代码可读性
1 parent 5fbffc5 commit e2df7ab

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.security.cert.CertificateFactory;
1818
import java.security.cert.CertificateNotYetValidException;
1919
import java.security.cert.X509Certificate;
20+
import java.time.Duration;
21+
import java.time.Instant;
2022
import java.util.ArrayList;
2123
import java.util.List;
2224
import java.util.concurrent.TimeUnit;
@@ -41,28 +43,28 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
4143
/**
4244
* 证书更新间隔时间,单位为分钟
4345
*/
44-
protected final int minutesInterval;
46+
protected final long minutesInterval;
4547
protected final Credentials credentials;
4648
protected final byte[] apiV3Key;
4749
protected final ReentrantLock lock = new ReentrantLock();
4850
/**
4951
* 上次更新时间
5052
*/
51-
protected volatile Long lastUpdate;
53+
protected volatile Instant lastUpdateTime;
5254
protected CertificatesVerifier verifier;
5355

5456
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key) {
55-
this(credentials, apiV3Key, (int) TimeUnit.HOURS.toMinutes(1));
57+
this(credentials, apiV3Key, TimeUnit.HOURS.toMinutes(1));
5658
}
5759

58-
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval) {
60+
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, long minutesInterval) {
5961
this.credentials = credentials;
6062
this.apiV3Key = apiV3Key;
6163
this.minutesInterval = minutesInterval;
6264
//构造时更新证书
6365
try {
6466
autoUpdateCert();
65-
lastUpdate = System.currentTimeMillis();
67+
lastUpdateTime = Instant.now();
6668
} catch (IOException | GeneralSecurityException e) {
6769
throw new RuntimeException(e);
6870
}
@@ -75,12 +77,13 @@ public X509Certificate getValidCertificate() {
7577

7678
@Override
7779
public boolean verify(String serialNumber, byte[] message, String signature) {
78-
if (lastUpdate == null || System.currentTimeMillis() - lastUpdate >= minutesInterval * 1000L * 60) {
80+
if (lastUpdateTime == null
81+
|| Duration.between(lastUpdateTime, Instant.now()).toMinutes() >= minutesInterval) {
7982
if (lock.tryLock()) {
8083
try {
8184
autoUpdateCert();
8285
//更新时间
83-
lastUpdate = System.currentTimeMillis();
86+
lastUpdateTime = Instant.now();
8487
} catch (GeneralSecurityException | IOException e) {
8588
log.warn("Auto update cert failed: ", e);
8689
} finally {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.IOException;
1212
import java.nio.charset.StandardCharsets;
1313
import java.time.DateTimeException;
14+
import java.time.Duration;
15+
import java.time.Instant;
1416
import org.apache.http.Header;
1517
import org.apache.http.HttpEntity;
1618
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -21,7 +23,10 @@
2123
public class WechatPay2Validator implements Validator {
2224

2325
protected static final Logger log = LoggerFactory.getLogger(WechatPay2Validator.class);
24-
26+
/**
27+
* 应答超时时间,单位为分钟
28+
*/
29+
protected static final long RESPONSE_EXPIRED_MINUTES = 5;
2530
protected final Verifier verifier;
2631

2732
public WechatPay2Validator(Verifier verifier) {
@@ -79,10 +84,9 @@ protected final void validateParameters(CloseableHttpResponse response) {
7984

8085
String timestampStr = header.getValue();
8186
try {
82-
long timestampInSecond = Long.parseLong(timestampStr);
83-
84-
// 拒绝5分钟之外的应答
85-
if (System.currentTimeMillis() / 1000L - timestampInSecond > 60 * 5) {
87+
Instant responseTime = Instant.ofEpochSecond(Long.parseLong(timestampStr));
88+
// 拒绝过期应答
89+
if (Duration.between(responseTime, Instant.now()).abs().toMinutes() >= RESPONSE_EXPIRED_MINUTES) {
8690
throw parameterError("timestamp=[%s] expires, request-id=[%s]", timestampStr, requestId);
8791
}
8892
} catch (DateTimeException | NumberFormatException e) {

0 commit comments

Comments
 (0)