提交 1797ef8a 编写于 作者: 小傅哥's avatar 小傅哥

feat: 字段加解密,MyBatis Plugin

上级 602c62ab
package cn.bugstack.xfg.dev.tech.plugin; package cn.bugstack.xfg.dev.tech.plugin;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Interceptor;
...@@ -13,18 +14,24 @@ import org.apache.ibatis.session.RowBounds; ...@@ -13,18 +14,24 @@ import org.apache.ibatis.session.RowBounds;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.lang.reflect.Field;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.Map;
@Intercepts({ @Intercepts({
@Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }), @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
}) })
public class FieldEncryptionAndDecryptionMybatisPlugin implements Interceptor { public class FieldEncryptionAndDecryptionMybatisPlugin implements Interceptor {
/** 密钥,必须是16位 */ /**
* 密钥,必须是16位
*/
private static final String KEY = "1898794876567654"; private static final String KEY = "1898794876567654";
/** 偏移量,必须是16位 */ /**
* 偏移量,必须是16位
*/
private static final String IV = "1233214566547891"; private static final String IV = "1233214566547891";
@Override @Override
...@@ -34,35 +41,41 @@ public class FieldEncryptionAndDecryptionMybatisPlugin implements Interceptor { ...@@ -34,35 +41,41 @@ public class FieldEncryptionAndDecryptionMybatisPlugin implements Interceptor {
Object parameter = args[1]; Object parameter = args[1];
String sqlId = mappedStatement.getId(); String sqlId = mappedStatement.getId();
if (parameter != null) { if (parameter != null && (sqlId.contains("insert") || sqlId.contains("update")) ) {
if (sqlId.contains("insert")) { String columnName = "employeeName";
// 插入操作,加密 if (parameter instanceof Map) {
String columnName = "employeeName"; List<Object> parameterList = (List<Object>) ((Map<?, ?>) parameter).get("list");
String fieldValue = BeanUtils.getProperty(parameter, columnName); for (Object obj : parameterList) {
String encryptedValue = encrypt(fieldValue); if (hasField(obj, columnName)) {
BeanUtils.setProperty(parameter, columnName, encryptedValue); String fieldValue = BeanUtils.getProperty(obj, columnName);
} else if (sqlId.contains("update")) { String encryptedValue = encrypt(fieldValue);
// 更新操作,加密 BeanUtils.setProperty(obj, columnName, encryptedValue);
String columnName = "employeeName"; }
String fieldValue = BeanUtils.getProperty(parameter, columnName); }
String encryptedValue = encrypt(fieldValue); } else {
BeanUtils.setProperty(parameter, columnName, encryptedValue); if (hasField(parameter, columnName)) {
String fieldValue = BeanUtils.getProperty(parameter, columnName);
String encryptedValue = encrypt(fieldValue);
BeanUtils.setProperty(parameter, columnName, encryptedValue);
}
} }
} }
Object result = invocation.proceed(); Object result = invocation.proceed();
if (result != null && sqlId.contains("query")) { if (result != null && sqlId.contains("query")) {
// 查询操作,解密 // 查询操作,解密
String columnName = "employeeName"; String columnName = "employeeName";
if (result instanceof List){ if (result instanceof List) {
List<Object> resultList = (List<Object>) result; List<Object> resultList = (List<Object>) result;
for (Object obj: resultList){ for (Object obj : resultList) {
String fieldValue = BeanUtils.getProperty(obj, columnName); if (!hasField(obj, columnName)) continue;
String decryptedValue = decrypt(fieldValue); String fieldValue = BeanUtils.getProperty(obj, columnName);
BeanUtils.setProperty(obj, columnName, decryptedValue); if (StringUtils.isBlank(fieldValue)) continue;
} String decryptedValue = decrypt(fieldValue);
} BeanUtils.setProperty(obj, columnName, decryptedValue);
}
}
} }
return result; return result;
...@@ -96,5 +109,17 @@ public class FieldEncryptionAndDecryptionMybatisPlugin implements Interceptor { ...@@ -96,5 +109,17 @@ public class FieldEncryptionAndDecryptionMybatisPlugin implements Interceptor {
return new String(original); return new String(original);
} }
public boolean hasField(Object obj, String fieldName) {
Class<?> clazz = obj.getClass();
while (clazz != null) {
try {
Field field = clazz.getDeclaredField(fieldName);
return true;
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
}
return false;
}
} }
...@@ -43,7 +43,7 @@ public class IEmployeeDAOTest { ...@@ -43,7 +43,7 @@ public class IEmployeeDAOTest {
@Test @Test
public void test_insert_list() { public void test_insert_list() {
List<EmployeePO> list = new ArrayList<>(); List<EmployeePO> list = new ArrayList<>();
for (int i = 0; i < 5; i++) { for (int i = 5; i < 8; i++) {
EmployeePO employee = new EmployeePO(); EmployeePO employee = new EmployeePO();
employee.setEmployeeNumber("1000001" + i); employee.setEmployeeNumber("1000001" + i);
employee.setEmployeeName("花花"); employee.setEmployeeName("花花");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册