-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
修复 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
Merged
Merged
Changes from 6 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
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
196 changes: 196 additions & 0 deletions
196
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,196 @@ | ||
| 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(); | ||
|
|
||
| System.out.println("=== Receiver 类中的所有字段 ==="); | ||
| boolean foundNameField = false; | ||
| boolean nameFieldHasAnnotation = false; | ||
|
|
||
| for (Field field : fields) { | ||
| System.out.println("字段名: " + field.getName() + ", 类型: " + field.getType().getName()); | ||
| if (field.getName().equals("name")) { | ||
| foundNameField = true; | ||
| if (field.isAnnotationPresent(SpecEncrypt.class)) { | ||
| nameFieldHasAnnotation = true; | ||
| System.out.println(" -> name 字段有 @SpecEncrypt 注解"); | ||
| } else { | ||
| System.out.println(" -> name 字段没有 @SpecEncrypt 注解"); | ||
| } | ||
| } | ||
| } | ||
|
binarywang marked this conversation as resolved.
Outdated
|
||
|
|
||
| // 验证能够找到 name 字段并且它有 @SpecEncrypt 注解 | ||
| assertTrue(foundNameField, "应该能找到 name 字段"); | ||
| assertTrue(nameFieldHasAnnotation, "name 字段应该有 @SpecEncrypt 注解"); | ||
| } | ||
|
|
||
| /** | ||
| * 测试嵌套对象中的字段加密 | ||
| * 验证 List<Receiver> 中每个 Receiver 对象的 name 字段是否能被正确找到和处理 | ||
| */ | ||
| @Test | ||
| public void testEncryptFieldsWithNestedObjects() { | ||
|
binarywang marked this conversation as resolved.
|
||
| // 创建测试对象 | ||
| 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); | ||
| System.out.println("ProfitSharingV3Request.receivers 字段有 @SpecEncrypt 注解: " + hasAnnotation); | ||
| assertTrue(hasAnnotation, "receivers 字段应该有 @SpecEncrypt 注解"); | ||
| } catch (NoSuchFieldException e) { | ||
| fail("应该能找到 receivers 字段"); | ||
| } | ||
|
|
||
| System.out.println("测试对象创建成功,name字段: " + receiver.getName()); | ||
|
binarywang marked this conversation as resolved.
Outdated
|
||
| // 验证name字段不为null | ||
| assertNotNull(receiver.getName()); | ||
| assertEquals(receiver.getName(), "张三"); | ||
| } | ||
|
binarywang marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * 测试单个对象中的字段加密 | ||
| * 验证直接在对象上的 @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); | ||
| System.out.println("ProfitSharingReceiverV3Request.name 字段有 @SpecEncrypt 注解: " + hasAnnotation); | ||
| assertTrue(hasAnnotation, "name 字段应该有 @SpecEncrypt 注解"); | ||
| } catch (NoSuchFieldException e) { | ||
| fail("应该能找到 name 字段"); | ||
| } | ||
|
|
||
| System.out.println("测试对象创建成功,name字段: " + request.getName()); | ||
|
binarywang marked this conversation as resolved.
Outdated
|
||
| // 验证name字段不为null | ||
| assertNotNull(request.getName()); | ||
| assertEquals(request.getName(), "李四"); | ||
| } | ||
|
binarywang marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * 测试类继承场景下的字段加密 | ||
| * 验证父类中带 @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.
|
||
| class ChildRequest extends ParentRequest { | ||
| @SpecEncrypt | ||
| @SerializedName("child_name") | ||
| private String childName; | ||
| } | ||
|
|
||
| // 创建子类实例 | ||
| ChildRequest request = new ChildRequest(); | ||
| request.setParentName("父类字段"); | ||
| request.setChildName("子类字段"); | ||
|
|
||
| // 验证能够找到父类和子类的字段 | ||
| System.out.println("=== 测试继承场景 ==="); | ||
| System.out.println("父类字段值: " + request.getParentName()); | ||
| System.out.println("子类字段值: " + request.getChildName()); | ||
|
|
||
| // 使用 getDeclaredFields 只能找到子类字段 | ||
| Field[] childFields = ChildRequest.class.getDeclaredFields(); | ||
| System.out.println("使用 getDeclaredFields 找到的字段数: " + childFields.length); | ||
|
|
||
| // 使用 getAllFields 辅助方法应该能找到父类和子类的所有字段 | ||
| List<Field> allFields = getAllFields(ChildRequest.class); | ||
|
binarywang marked this conversation as resolved.
Outdated
|
||
| System.out.println("使用 getAllFields 找到的字段数: " + allFields.size()); | ||
|
|
||
| int annotatedFieldCount = 0; | ||
| for (Field field : allFields) { | ||
| if (field.isAnnotationPresent(SpecEncrypt.class)) { | ||
| annotatedFieldCount++; | ||
| System.out.println(" -> 找到带 @SpecEncrypt 注解的字段: " + field.getName()); | ||
|
binarywang marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| // 应该找到2个带注解的字段(parentName 和 childName) | ||
| assertTrue(annotatedFieldCount >= 2, "应该能找到至少2个带 @SpecEncrypt 注解的字段"); | ||
| } | ||
|
|
||
| /** | ||
| * 辅助方法:递归获取类的所有字段,包括父类中的字段 | ||
| */ | ||
| private List<Field> getAllFields(Class<?> clazz) { | ||
| List<Field> fields = new ArrayList<>(); | ||
| while (clazz != null && clazz != Object.class) { | ||
| Field[] declaredFields = clazz.getDeclaredFields(); | ||
| java.util.Collections.addAll(fields, declaredFields); | ||
| clazz = clazz.getSuperclass(); | ||
| } | ||
| return fields; | ||
| } | ||
|
binarywang marked this conversation as resolved.
Outdated
|
||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.