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

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

}

相关推荐
微信开发api-视频号协议17 小时前
企业微信外部群开发自动化实践过程
java·前端·微信·自动化·企业微信·ipad
微信开发api-视频号协议3 天前
企业微信二次开发中的文件系统设计:媒体资源、临时文件与业务附件
前端·微信·企业微信·媒体·ipad·微信开放平台
微信开发api-视频号协议3 天前
Codex++安全边界探秘:从模型能力到风险防御
前端·安全·微信·企业微信
caimouse3 天前
reactos 测试安装32位微信失败的日志
开发语言·微信
毛丫讲绘本4 天前
0-3岁选绘本需要做到越早启蒙越要简单
人工智能·学习·微信·微信公众平台·微信开放平台
中云DDoS CC防护蔡蔡4 天前
短信验证码被攻击怎么办
运维·经验分享·http·网络安全·微信
金銀銅鐵4 天前
如何在微信对话框里发送“空白”信息?
微信
张居斜14 天前
Obsidian + Claude Code + 微信AI,我把这三个系统缝进了一个软件
微信·obsidian·claude code·molio
Jinkey16 天前
要用户手机号真的是为了打骚扰电话吗?浅谈微信生态会员账号体系与资产合并
后端·微信·微信小程序
2601_9618451521 天前
粉笔行测题库|系统班|刷题
网络·百度·微信·微信公众平台·facebook·新浪微博