修复 RsaCryptoUtil 无法加密继承字段和嵌套对象的问题#3841
Merged
binarywang merged 9 commits intodevelopfrom Jan 15, 2026
Merged
Conversation
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix RsaCryptoUtil to encrypt subclass fields
修复 RsaCryptoUtil 无法加密继承字段和嵌套对象的问题
Jan 9, 2026
There was a problem hiding this comment.
Pull request overview
此 PR 修复了 RsaCryptoUtil.encryptFields() 方法无法加密父类继承字段和嵌套对象字段的问题。原有实现使用 getDeclaredFields() 只能获取当前类声明的字段,导致父类字段和集合元素内部的加密字段被忽略。
主要变更:
- 新增
getAllFields()辅助方法,递归获取类及其所有父类的字段 - 修改
encryptField()方法,使用getAllFields()替代getDeclaredFields() - 新增测试类
RsaCryptoUtilTest,包含嵌套对象、继承场景和反射验证的测试用例
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtil.java | 新增 getAllFields() 方法支持获取继承字段,修改 encryptField() 使用新方法 |
| weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java | 新增测试类,包含嵌套对象、直接字段和继承场景的测试用例 |
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtil.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
🤖 Augment PR SummarySummary: 本 PR 修复了微信支付 v3 敏感信息加密在“继承字段/嵌套对象”场景下无法生效的问题,确保分账等请求里被 @SpecEncrypt 标注的字段能被正确处理。 Changes:
Technical Notes: 通过在遍历时于 🤖 Was this summary useful? React with 👍 or 👎 |
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Show resolved
Hide resolved
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
Outdated
Show resolved
Hide resolved
…til/RsaCryptoUtil.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…til/RsaCryptoUtilTest.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Owner
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Contributor
Author
已根据 PR Review 反馈完成所有修改:
所有测试通过,Checkstyle 检查通过。提交: bd0ba53 |
Owner
|
auggie review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复 RSA 加密工具无法加密嵌套对象字段的问题 ✅
问题描述
RsaCryptoUtil.encryptFields只能加密当前类中的@SpecEncrypt字段,无法加密集合中嵌套对象的字段RsaCryptoUtil.encryptField方法以支持继承字段最新改进(基于 PR Review 反馈)
System.out.println调试语句getAllFields()方法,改用反射调用生产代码中的私有方法ChildRequest.canEqual()方法添加@Override注解技术细节与修复
根本原因
原实现使用
Class.getDeclaredFields()方法,该方法只返回当前类中声明的字段,不包括从父类继承的字段。修复方案
新增
getAllFields()辅助方法,递归遍历类的继承链,获取包括父类在内的所有字段。代码修改
性能优化
Collections.addAll()批量添加字段Object.class处停止遍历,避免处理不必要的系统字段测试覆盖
创建了完整的单元测试套件(4个测试用例):
所有测试均通过 ✅
代码质量验证
实际影响
此修复解决了用户报告的核心问题:
ProfitSharingV3Request中receivers集合内每个Receiver对象的name字段现在可以被正确加密@SpecEncrypt字段也能被正确处理向后兼容性
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.