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

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

}

相关推荐
@Ma17 小时前
企业微信外部群机器人接入 AI:一套能落地的工程方案
微信·机器人
2601_9611940220 小时前
27考研资料|免费全套|电子版
考研·百度·微信·pdf·微信公众平台·facebook·新浪微博
凌奕2 天前
微信小程序接入微信 AI:让用户"说一句话"就能下单
微信·微信小程序·agent
弓乙图3 天前
弓乙歌/岐黄真源赋
经验分享·微信
2601_961194023 天前
初中英语教资笔试资源|科三教案模板和知识点资料
百度·微信·微信公众平台·facebook·twitter·新浪微博
梁辰兴4 天前
微信小程序开发者工具下载与安装
微信·微信小程序·小程序·教程·软件安装·开发者工具
Thomas_YXQ6 天前
Unity无GC读取图片与网格完整方案
大数据·人工智能·unity·微信·产品运营
山川而川-R6 天前
调用微信开源二维码模型
微信·开源
凌涘7 天前
依托 BEM 规范深度剖析 WeUI 微信按钮组件开发与实现
前端·微信
m0_696212687 天前
如何用微信 API 实现多群消息同步和定时群发
微信