Repository navigation
支持微信支付 V3 SM2/SM3 签名与验签 - #4151
binarywang wants to merge 2 commits into
Conversation
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bae282bbce
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| default boolean verify(String serialNumber, byte[] message, String signature, String signatureType) { | ||
| return verify(serialNumber, message, signature); |
There was a problem hiding this comment.
当使用 VerifierBuilder 创建的默认平台证书验签器处理 SM2 响应时,WxPayValidator 虽然调用了四参数 verify,但 AutoUpdateCertificatesVerifier 没有覆盖该重载,因此会执行这里的默认实现,丢弃 signatureType。其三参数方法随后调用 CertificatesVerifier.verify 的三参数版本,最终强制使用 RSA,导致有效的 SM2 响应验签失败或抛出无效证书异常。需要在 AutoUpdateCertificatesVerifier 中覆盖四参数重载,在完成证书更新检查后向内部验签器透传签名类型,并补充覆盖这条默认构建链路的测试。
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
🟡 Changes recommended
Signature types are not propagated through the certificate wrapper and notification verification paths, causing valid SM2 signatures to be incorrectly verified as RSA and fail.
2 open issues
What changed in this PR
本 PR 为 WxJava 微信支付 V3 的请求签名和响应验签增加 SM2/SM3 支持,对应 #4141,并保留现有 RSA 行为。
变更:
- 新增统一签名算法工具,支持 SM2/SM3、指定用户 ID 和动态认证类型。
- 根据响应签名类型选择验签算法,并扩展 PEM 工具以解析 EC 密钥。
- 新增 SM2 签名、验签、密钥解析及 RSA 兼容性测试,并注册到 TestNG 套件。
验证范围:已静态核查相关差异和调用链,未执行构建或测试。
| File | Description |
|---|---|
weixin-java-pay/src/test/resources/testng.xml |
注册新增 SM2 测试类。 |
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/auth/Sm2SignatureTest.java |
覆盖 SM2 功能及 RSA 兼容性。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/PemUtils.java |
增加 EC 私钥和公钥解析。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java |
读取并校验响应签名类型,传入验证器。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java |
从签名器获取认证类型。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WechatPaySignature.java |
集中创建 RSA、SM2 签名对象并设置用户 ID。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/Verifier.java |
增加带签名类型的验签重载。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/Signer.java |
增加默认 RSA 认证类型接口。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/PublicCertificateVerifier.java |
支持按指定算法进行公钥验签和证书委托。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/PrivateKeySigner.java |
根据私钥类型选择签名算法和认证类型。 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/CertificatesVerifier.java |
增加按指定算法进行证书验签的路径。 |
Review effort: Balanced
|
|
||
| @Override | ||
| public boolean verify(String serialNumber, byte[] message, String signature) { | ||
| return verify(serialNumber, message, signature, WechatPaySignature.RSA_TYPE); |
| default boolean verify(String serialNumber, byte[] message, String signature, String signatureType) { | ||
| return verify(serialNumber, message, signature); |

微信支付 API v3 增加国密签名支持,按官方规则使用 SM2-with-SM3、指定用户 ID,并通过
Wechatpay-Signature-Type区分响应签名算法。WECHATPAY2-SM2-WITH-SM3;RSA 签名保持原有行为。