修复培训:修复学员身份保护组件Spring构造器注入
This commit is contained in:
1 parent
08d9a00a0e
commit
3f1e3b2970
2 files changed
+31
No files matched your search
+2
@@ -10,6 +10,7 @@ import javax.crypto.Cipher;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -34,6 +35,7 @@ public class JceStudentIdentityProtection implements StudentIdentityProtection
|
||||
private final byte[] hashKey;
|
||||
private final SecureRandom secureRandom;
|
||||
|
||||
@Autowired
|
||||
public JceStudentIdentityProtection(
|
||||
@Value("${train.student.identity.encryption-key:}") String encryptionKeyBase64,
|
||||
@Value("${train.student.identity.hash-key:}") String hashKeyBase64)
|
||||
|
||||
+29
@@ -3,11 +3,16 @@ package com.ruoyi.train.security;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
/** 身份证加密、确定性查重与脱敏规则单元测试。 */
|
||||
public class JceStudentIdentityProtectionTest
|
||||
@@ -77,4 +82,28 @@ public class JceStudentIdentityProtectionTest
|
||||
new JceStudentIdentityProtection(Base64.getEncoder().encodeToString(new byte[32]),
|
||||
Base64.getEncoder().encodeToString(new byte[16]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInstantiateAsSpringBeanWithExplicitAutowiredConstructor()
|
||||
{
|
||||
byte[] testEncKey = new byte[32];
|
||||
byte[] testHashKey = new byte[32];
|
||||
Arrays.fill(testEncKey, (byte) 0x11);
|
||||
Arrays.fill(testHashKey, (byte) 0x22);
|
||||
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("train.student.identity.encryption-key", Base64.getEncoder().encodeToString(testEncKey));
|
||||
properties.put("train.student.identity.hash-key", Base64.getEncoder().encodeToString(testHashKey));
|
||||
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext())
|
||||
{
|
||||
context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("testProps", properties));
|
||||
context.register(JceStudentIdentityProtection.class);
|
||||
context.refresh();
|
||||
|
||||
StudentIdentityProtection bean = context.getBean(StudentIdentityProtection.class);
|
||||
assertNotNull("Spring 容器应通过显式 @Autowired 构造器成功实例化 Bean", bean);
|
||||
assertEquals("110101********1234", bean.mask(ID_CARD));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user