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

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

}

相关推荐
微擎应用市场1 天前
掌盟网络答题游戏模块:微信公众号互动营销新选择
微信
微擎应用市场1 天前
V2 积分商城小程序系统:一站式积分兑换解决方案
微信
微擎应用市场1 天前
全方位赋能小微企业:KK-CRM 客户关系管理软件详细介绍
微信
微擎应用市场1 天前
新视角家政系统:一站式上门服务数字化解决方案
微信
陈思杰系统思考Jason2 天前
系统思考:团队学习
百度·微信·微信公众平台·新浪微博·微信开放平台
陈思杰系统思考Jason3 天前
内在动力缺失与组织平庸
百度·微信·微信公众平台·新浪微博·微信开放平台
wan55cn@126.com5 天前
人生如戏:换个片场,继续出演
人工智能·笔记·百度·微信
從南走到北6 天前
JAVA代驾小程序源码代驾跑腿APP源码
java·开发语言·微信·微信小程序·小程序
太过平凡的小蚂蚁8 天前
责任链模式:灵活可扩展的责任传递艺术(行为模式)
微信·责任链模式
孙严Pay8 天前
快捷支付和网关支付到底哪个更安全呢?
笔记·科技·计算机网络·其他·微信