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

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

}

相关推荐
陈思杰系统思考Jason6 小时前
系统思考与简单执行
百度·微信·微信公众平台·新浪微博·微信开放平台
悟空码字12 小时前
【保姆级】实现APP分享至微信,看完就能落地
java·后端·微信
羊群智妍1 天前
2026年GEO服务商推荐:SheepGeo免费AI搜索优化监测工具
百度·微信·微信公众平台·facebook·新浪微博
Ealrang2 天前
openclaw连微信看不到二维码的看这里
微信
一个人旅程~2 天前
虚数与量子迷踪
经验分享·笔记·微信·电脑·量子计算
feelling3 天前
微信官方接入 OpenClaw?我 10 分钟搞定了,但过程中这几个坑你必须知道
微信
陈思杰系统思考Jason3 天前
系统思考:结构性缺陷与企业波动
百度·微信·微信公众平台·新浪微博·微信开放平台
陈思杰系统思考Jason3 天前
动态博弈中的系统思考
百度·微信·微信公众平台·新浪微博·微信开放平台
Refly3 天前
【微信接入 OpenClaw 龙虾🦞】10分钟手把手教程完成接入,Claude 模型无限使用
前端·微信·github
AI袋鼠帝4 天前
劲爆!个人微信官方接入龙虾了【喂饭级教程】
人工智能·微信