Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微工卡并发调用PayrollCardService#getRelation验签失败 #114

Closed
zhangjs28 opened this issue Jan 6, 2023 · 0 comments · Fixed by #115
Closed

微工卡并发调用PayrollCardService#getRelation验签失败 #114

zhangjs28 opened this issue Jan 6, 2023 · 0 comments · Fixed by #115
Assignees
Labels
bug Something isn't working

Comments

@zhangjs28
Copy link

zhangjs28 commented Jan 6, 2023

错误描述

发生验签失败
https://developers.weixin.qq.com/community/pay/doc/00066ef2658830a0901f83a5e51800?fromCreate=1

重现bug的步骤

在并发查询时发生验签失败异常

预期行为

验签不应该失败

导致错误的代码片段

package com.wechat.pay.java.core.cipher;

import static java.util.Objects.requireNonNull;

import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Base64;

public abstract class AbstractSigner implements Signer {

  private final String certificateSerialNumber;
  private final String algorithm;
  private final Signature signature;

  /**
   * AbstractSigner 构造函数
   *
   * @param algorithm 微信支付的签名算法,例如SHA256-RSA2048
   * @param algorithmName 获取Signature对象时指定的算法,例如SHA256withRSA
   * @param certificateSerialNumber 商户API证书序列号
   * @param privateKey 商户API私钥
   */
  protected AbstractSigner(
      String algorithm,
      String algorithmName,
      String certificateSerialNumber,
      PrivateKey privateKey) {
    this.algorithm = requireNonNull(algorithm);
    this.certificateSerialNumber = requireNonNull(certificateSerialNumber);
    try {
      this.signature = Signature.getInstance(algorithmName);
      this.signature.initSign(privateKey);
    } catch (NoSuchAlgorithmException e) {
      throw new UnsupportedOperationException(
          "The current Java environment does not support " + algorithmName, e);
    } catch (InvalidKeyException e) {
      throw new IllegalArgumentException(algorithm + " signature uses an illegal privateKey.", e);
    }
  }

  @Override
  public SignatureResult sign(String message) {
    requireNonNull(message);
    byte[] sign;
    try {
      signature.update(message.getBytes(StandardCharsets.UTF_8));
      sign = signature.sign();
    } catch (SignatureException e) {
      throw new RuntimeException("An error occurred during the sign process.", e);
    }
    return new SignatureResult(Base64.getEncoder().encodeToString(sign), certificateSerialNumber);
  }

  @Override
  public String getAlgorithm() {
    return algorithm;
  }
}

操作系统

linux

Java 版本

1.8

wechatpay-java 版本

0.2.3

其他信息

@Override
    public SignatureResult sign(String message) {
        requireNonNull(message);
        byte[] sign;
        try {
            Signature signature = Signature.getInstance(algorithmName);
            signature.initSign(privateKey);
            signature.update(message.getBytes(StandardCharsets.UTF_8));

            sign = signature.sign();
        } catch (NoSuchAlgorithmException e) {
            throw new UnsupportedOperationException(
                    "The current Java environment does not support " + algorithmName, e);
        } catch (InvalidKeyException e) {
            throw new IllegalArgumentException(algorithm + " signature uses an illegal privateKey.", e);
        } catch (SignatureException e) {
            throw new RuntimeException("An error occurred during the sign process.", e);
        }
        return new SignatureResult(Base64.getEncoder().encodeToString(sign), certificateSerialNumber);
    }

Signature对象获取改到com.huixian.alpharisk.util.wgk.WgkSigner#sign中,生成的签名,并发调用不再发生验签失败

@zhangjs28 zhangjs28 added the bug Something isn't working label Jan 6, 2023
xy-peng added a commit that referenced this issue Jan 6, 2023
xy-peng added a commit that referenced this issue Jan 6, 2023
xy-peng added a commit that referenced this issue Jan 6, 2023
* fix: 修复AbstractSigner并发错误

resolve #114

* bump version v0.2.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants