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

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

}

相关推荐
罗光记20 分钟前
Solon Server 启动模式深度解析:从 0.3MB 内核到 10+ Server 插件
其他·百度·微信·微信公众平台·新浪微博
阳明山水3 天前
销量预测模型评估进阶:从 MAPE 到库存周转率与缺货损失金额
人工智能·机器学习·微信·微信公众平台·微信开放平台
wan55cn@126.com4 天前
调试协作之歌
人工智能·笔记·微信
柚鸥ASO优化5 天前
微信正在变成“搜索引擎”:小程序SEO机会全面爆发
搜索引擎·微信·小程序·小程序优化
写了20年代码的老程序员6 天前
企业微信、飞书、钉钉 Webhook 接入,后端代码为什么总是越写越丑
java·微信
px不是xp9 天前
【灶台导航】个人中心模块开发实战
javascript·微信·腾讯云·notepad++
User_芊芊君子10 天前
KouriChat + DeepSeek + 微信接入:本地 AI 角色聊天机器人搭建实录
人工智能·微信·机器人
咬人喵喵11 天前
E2编辑器里的零高容器是什么?怎么用?
低代码·微信·编辑器·交互·svg
虎头金猫12 天前
管理飞牛OS还在点点点?我用Ansible给它装了个远程遥控器
微信·开源·ansible·aigc·智能家居·开源软件·ai编程
Szime12 天前
深智微华润微代理一线实战CS57167半桥驱动在无人机电调中的国产替代选型指南
微信