-
-
Notifications
You must be signed in to change notification settings - Fork 9k
修复 RsaCryptoUtil 无法加密继承字段和嵌套对象的问题 #3841
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
Open
Copilot
wants to merge
9
commits into
develop
Choose a base branch
from
copilot/fix-rsa-encryption-bug
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−2
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8c23292
Initial plan
Copilot 89572f0
创建初始计划
Copilot ccd1936
添加测试以验证加密逻辑
Copilot 6a9d6c6
修复 RsaCryptoUtil 以支持继承字段的加密
Copilot f048e7c
修复代码风格:使用合适的导入而不是完全限定类名
Copilot 9b23730
性能优化:改进 getAllFields 方法的效率并避免遍历 Object 类
Copilot 487e197
Update weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/u…
binarywang a256272
Update weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/u…
binarywang bd0ba53
清理测试代码:移除调试输出,使用反射调用生产代码,添加 @Override 注解
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
179 changes: 179 additions & 0 deletions
179
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package com.github.binarywang.wxpay.v3.util; | ||
|
|
||
| import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingReceiverV3Request; | ||
| import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingV3Request; | ||
| import com.github.binarywang.wxpay.exception.WxPayException; | ||
| import com.github.binarywang.wxpay.v3.SpecEncrypt; | ||
| import com.google.gson.annotations.SerializedName; | ||
| import lombok.Data; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.testng.Assert.*; | ||
|
|
||
| /** | ||
| * RsaCryptoUtil 测试类 | ||
| */ | ||
| public class RsaCryptoUtilTest { | ||
|
|
||
| /** | ||
| * 测试反射能否找到嵌套类中的 @SpecEncrypt 注解字段 | ||
| */ | ||
| @Test | ||
| public void testFindAnnotatedFieldsInNestedClass() { | ||
| // 创建 Receiver 对象 | ||
| ProfitSharingV3Request.Receiver receiver = new ProfitSharingV3Request.Receiver(); | ||
| receiver.setName("测试姓名"); | ||
|
|
||
| // 使用反射查找带有 @SpecEncrypt 注解的字段 | ||
| Class<?> receiverClass = receiver.getClass(); | ||
| Field[] fields = receiverClass.getDeclaredFields(); | ||
|
|
||
| boolean foundNameField = false; | ||
| boolean nameFieldHasAnnotation = false; | ||
|
|
||
| for (Field field : fields) { | ||
| if (field.getName().equals("name")) { | ||
| foundNameField = true; | ||
| if (field.isAnnotationPresent(SpecEncrypt.class)) { | ||
| nameFieldHasAnnotation = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 验证能够找到 name 字段并且它有 @SpecEncrypt 注解 | ||
| assertTrue(foundNameField, "应该能找到 name 字段"); | ||
| assertTrue(nameFieldHasAnnotation, "name 字段应该有 @SpecEncrypt 注解"); | ||
| } | ||
|
|
||
| /** | ||
| * 测试嵌套对象中的字段加密 | ||
| * 验证 List<Receiver> 中每个 Receiver 对象的 name 字段是否能被正确找到和处理 | ||
| */ | ||
| @Test | ||
| public void testEncryptFieldsWithNestedObjects() { | ||
| // 创建测试对象 | ||
| ProfitSharingV3Request request = ProfitSharingV3Request.newBuilder() | ||
| .appid("test-appid") | ||
| .subMchId("test-submchid") | ||
| .transactionId("test-transaction") | ||
| .outOrderNo("test-order-no") | ||
| .unfreezeUnsplit(true) | ||
| .build(); | ||
|
|
||
| List<ProfitSharingV3Request.Receiver> receivers = new ArrayList<>(); | ||
| ProfitSharingV3Request.Receiver receiver = new ProfitSharingV3Request.Receiver(); | ||
| receiver.setName("张三"); // 设置需要加密的字段 | ||
| receiver.setAccount("test-account"); | ||
| receiver.setType("PERSONAL_OPENID"); | ||
| receiver.setAmount(100); | ||
| receiver.setRelationType("STORE"); | ||
| receiver.setDescription("测试分账"); | ||
|
|
||
| receivers.add(receiver); | ||
| request.setReceivers(receivers); | ||
|
|
||
| // 验证 receivers 字段有 @SpecEncrypt 注解 | ||
| try { | ||
| Field receiversField = ProfitSharingV3Request.class.getDeclaredField("receivers"); | ||
| boolean hasAnnotation = receiversField.isAnnotationPresent(SpecEncrypt.class); | ||
| assertTrue(hasAnnotation, "receivers 字段应该有 @SpecEncrypt 注解"); | ||
| } catch (NoSuchFieldException e) { | ||
| fail("应该能找到 receivers 字段"); | ||
| } | ||
|
|
||
| // 验证name字段不为null | ||
| assertNotNull(receiver.getName()); | ||
| assertEquals(receiver.getName(), "张三"); | ||
| } | ||
|
|
||
| /** | ||
| * 测试单个对象中的字段加密 | ||
| * 验证直接在对象上的 @SpecEncrypt 字段是否能被正确找到 | ||
| */ | ||
| @Test | ||
| public void testEncryptFieldsWithDirectField() { | ||
| // 创建测试对象 | ||
| ProfitSharingReceiverV3Request request = ProfitSharingReceiverV3Request.newBuilder() | ||
| .appid("test-appid") | ||
| .subMchId("test-submchid") | ||
| .type("PERSONAL_OPENID") | ||
| .account("test-account") | ||
| .name("李四") | ||
| .relationType("STORE") | ||
| .build(); | ||
|
|
||
| // 验证 name 字段有 @SpecEncrypt 注解 | ||
| try { | ||
| Field nameField = ProfitSharingReceiverV3Request.class.getDeclaredField("name"); | ||
| boolean hasAnnotation = nameField.isAnnotationPresent(SpecEncrypt.class); | ||
| assertTrue(hasAnnotation, "name 字段应该有 @SpecEncrypt 注解"); | ||
| } catch (NoSuchFieldException e) { | ||
| fail("应该能找到 name 字段"); | ||
| } | ||
|
|
||
| // 验证name字段不为null | ||
| assertNotNull(request.getName()); | ||
| assertEquals(request.getName(), "李四"); | ||
| } | ||
|
|
||
| /** | ||
| * 测试类继承场景下的字段加密 | ||
| * 验证父类中带 @SpecEncrypt 注解的字段是否能被正确找到和加密 | ||
| */ | ||
| @Test | ||
| public void testEncryptFieldsWithInheritance() { | ||
| // 定义测试用的父类和子类 | ||
| @Data | ||
| class ParentRequest { | ||
| @SpecEncrypt | ||
| @SerializedName("parent_name") | ||
| private String parentName; | ||
| } | ||
|
|
||
| @Data | ||
| @lombok.EqualsAndHashCode(callSuper = false) | ||
binarywang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| class ChildRequest extends ParentRequest { | ||
| @SpecEncrypt | ||
| @SerializedName("child_name") | ||
| private String childName; | ||
|
|
||
| @Override | ||
| protected boolean canEqual(final Object other) { | ||
| return other instanceof ChildRequest; | ||
| } | ||
| } | ||
|
|
||
| // 创建子类实例 | ||
| ChildRequest request = new ChildRequest(); | ||
| request.setParentName("父类字段"); | ||
| request.setChildName("子类字段"); | ||
|
|
||
| // 验证能够找到父类和子类的字段 | ||
| // 使用 getDeclaredFields 只能找到子类字段 | ||
| Field[] childFields = ChildRequest.class.getDeclaredFields(); | ||
|
|
||
| // 使用反射调用 RsaCryptoUtil 的私有 getAllFields 方法 | ||
| int annotatedFieldCount = 0; | ||
| try { | ||
| java.lang.reflect.Method getAllFieldsMethod = RsaCryptoUtil.class.getDeclaredMethod("getAllFields", Class.class); | ||
| getAllFieldsMethod.setAccessible(true); | ||
| @SuppressWarnings("unchecked") | ||
| List<Field> allFields = (List<Field>) getAllFieldsMethod.invoke(null, ChildRequest.class); | ||
|
|
||
| for (Field field : allFields) { | ||
| if (field.isAnnotationPresent(SpecEncrypt.class)) { | ||
| annotatedFieldCount++; | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| fail("无法调用 getAllFields 方法: " + e.getMessage()); | ||
| } | ||
|
|
||
| // 应该找到2个带注解的字段(parentName 和 childName) | ||
| assertTrue(annotatedFieldCount >= 2, "应该能找到至少2个带 @SpecEncrypt 注解的字段"); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个测试目前只验证注解存在/字段赋值,并未实际调用
RsaCryptoUtil.encryptFields()或断言字段已被加密,因此即使修复逻辑回退测试也可能仍然通过(同类问题也出现在本文件的其他测试方法中)。🤖 Was this useful? React with 👍 or 👎