解决微信支付定时更新证书间隔问题

package com.wechat.pay.contrib.apache.httpclient.auth;

import com.wechat.pay.contrib.apache.httpclient.Credentials;

import com.wechat.pay.contrib.apache.httpclient.cert.CertManagerSingleton;

import java.security.cert.X509Certificate;

import java.util.concurrent.locks.ReentrantLock;

/**

  • 在原有CertificatesVerifier基础上,增加定时更新证书功能(默认1小时)

  • @author lianup

  • @since 0.3.0

    */

    public class ScheduledUpdateCertificatesVerifier implements Verifier {

    protected static final int UPDATE_INTERVAL_MINUTE = 60;

    private final ReentrantLock lock;

    private final CertManagerSingleton certManagerSingleton;

    private final CertificatesVerifier verifier;

    public ScheduledUpdateCertificatesVerifier(Credentials credentials, byte[] apiv3Key) {

    lock = new ReentrantLock();

    certManagerSingleton = CertManagerSingleton.getInstance();

    initCertManager(credentials, apiv3Key);

    verifier = new CertificatesVerifier(certManagerSingleton.getCertificates());

    }

    public void initCertManager(Credentials credentials, byte[] apiv3Key) {

    if (credentials == null || apiv3Key.length == 0) {

    throw new IllegalArgumentException("credentials或apiv3Key为空");

    }

    certManagerSingleton.init(credentials, apiv3Key, UPDATE_INTERVAL_MINUTE);

    }

    @Override

    public X509Certificate getLatestCertificate() {

    return certManagerSingleton.getLatestCertificate();

    }

    @Override

    public boolean verify(String serialNumber, byte[] message, String signature) {

    if (serialNumber.isEmpty() || message.length == 0 || signature.isEmpty()) {

    throw new IllegalArgumentException("serialNumber或message或signature为空");

    }

    if (lock.tryLock()) {

    try {

    verifier.updateCertificates(certManagerSingleton.getCertificates());

    } finally {

    lock.unlock();

    }

    }

    return verifier.verify(serialNumber, message, signature);

    }

    /**

    • 该方法已废弃,请勿使用
    • @return null
      */
      @Deprecated
      @Override
      public X509Certificate getValidCertificate() {
      return null;
      }

    /**

    • 停止定时更新,停止后无法再重新启动
      */
      public void stopScheduledUpdate() {
      certManagerSingleton.close();
      }

}

相关推荐
j七七8 小时前
5分钟搭微信自动回复机器人5分钟搭微信自动回复机器人
运维·服务器·开发语言·前端·python·微信
從南走到北11 小时前
JAVA国际版任务悬赏发布接单系统源码支持IOS+Android+H5
android·java·ios·微信·微信小程序·小程序
黑马源码库miui520861 天前
JAVA购物返利商品比价系统源码支持微信小程序
微信·微信小程序·小程序·1024程序员节
黑马源码库miui520862 天前
场馆预定系统小程序
微信·微信小程序·小程序·1024程序员节
從南走到北3 天前
JAVA无人自助共享系统台球室源码自助开台约球交友系统源码小程序
java·微信·微信小程序·小程序·1024程序员节
從南走到北4 天前
JAVA国际版一对一视频交友视频聊天系统源码支持H5 + APP
java·微信·微信小程序·小程序·音视频·交友
從南走到北4 天前
同城派送小程序
微信·微信小程序·小程序
陈思杰系统思考Jason5 天前
系统思考重塑工作效能
百度·微信·微信公众平台·新浪微博·微信开放平台
hookserver6 天前
企业微信ipad协议接口优势
http·ios·微信·企业微信·ipad·企微
hookserver6 天前
企业微信聚合应用系统,ipad协议接口
java·http·微信·企业微信·ipad