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

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();
      }

}

相关推荐
陈思杰系统思考Jason13 小时前
系统思考:团队学习与成长飞轮
百度·微信·微信公众平台·新浪微博·微信开放平台
IT WorryFree1 天前
如何设置微信群机器人
微信·github
非优秀程序员1 天前
10分钟,用qclaw打造你的AI选股系统--官方每天送4百万token
人工智能·微信·产品经理
AI周红伟2 天前
周红伟:OpenClaw 企业智能体:架构、治理与全球部署实战
人工智能·微信·架构·云计算·腾讯云·openclaw
gsls2008082 天前
ubuntu安装openclaw接入智谱大模型和微信QQ通道配置
微信·qq·智谱·openclaw·龙虾
AI周红伟2 天前
周红伟:OpenClaw 企业级智能体架构与全栈实战
人工智能·微信·架构·云计算·腾讯云·openclaw
陈思杰系统思考Jason2 天前
局限思考的代价:团队内耗
百度·微信·微信公众平台·新浪微博·微信开放平台
Crazy CodeCrafter2 天前
房租年年涨,客流年年少,服装店还要开吗?
大数据·运维·微信·自动化·开源软件
陈思杰系统思考Jason2 天前
系统思考:组织学习与个人学习
百度·微信·微信公众平台·新浪微博·微信开放平台
羊群智妍3 天前
2026年AI搜索优化监测工具横评:免费到付费方案
百度·微信·微信公众平台·facebook·新浪微博