1717import java .security .cert .CertificateFactory ;
1818import java .security .cert .CertificateNotYetValidException ;
1919import java .security .cert .X509Certificate ;
20+ import java .time .Duration ;
21+ import java .time .Instant ;
2022import java .util .ArrayList ;
2123import java .util .List ;
2224import 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 {
0 commit comments