mybatis攔截器自動加密解密教程
更新時間:2025年08月08日 11:19:36 作者:碼農(nóng)本農(nóng)。
這篇文章主要介紹了mybatis攔截器自動加密解密案例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
mybatis攔截器自動加密解密
來看實例
import kai8.system.annotation.EncryptDecryptField;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.util.Objects;
@Slf4j
@Component
@Intercepts({
@Signature(type = ParameterHandler.class, method = "setParameters", args = PreparedStatement.class)
})
public class EncryptionInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
ParameterHandler item = (ParameterHandler) invocation.getTarget();
Field parameterObject = item.getClass().getDeclaredField("parameterObject");
parameterObject.setAccessible(true);
Object o = parameterObject.get(item);
if (!Objects.isNull(o)) {
Class<?> aClass = o.getClass();
for (; aClass != Object.class; aClass = aClass.getSuperclass()) {//向上循環(huán) 遍歷父類
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
// 如果屬性帶有EncryptField注解放到要加解密的集合中
if (field.isAnnotationPresent(EncryptDecryptField.class)) {
field.setAccessible(true);
try {
// 假設你有某種方法獲取加密值(根據(jù)你的邏輯)
String encryptedValue = (String) field.get(o);
if (encryptedValue != null && !"".equals(encryptedValue)) {
EncryptDecryptField annotation = field.getAnnotation(EncryptDecryptField.class);
Class<?> value = annotation.value();
Method method = value.getMethod(annotation.encrypt(), String.class);
Object invoke = method.invoke(value, encryptedValue);
field.set(o, invoke);
} else {
field.set(o, null);
}
} catch (IllegalAccessException e) {
// 處理異常
log.error(e.getMessage(), e);
}
}
}
}
}
return invocation.proceed();
}
}import kai8.system.annotation.EncryptDecryptField;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Statement;
import java.util.Objects;
@Slf4j
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class DecryptInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
ParameterHandler item = (ParameterHandler) invocation.getTarget();
Field parameterObject = item.getClass().getDeclaredField("parameterObject");
parameterObject.setAccessible(true);
Object o = parameterObject.get(item);
if (!Objects.isNull(o)) {
Class<?> aClass = o.getClass();
for (; aClass != Object.class; aClass = aClass.getSuperclass()) {//向上循環(huán) 遍歷父類
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
// 如果屬性帶有EncryptField注解放到要加解密的集合中
if (field.isAnnotationPresent(EncryptDecryptField.class)) {
field.setAccessible(true);
try {
// 假設你有某種方法獲取加密值(根據(jù)你的邏輯)
String encryptedValue = (String) field.get(o);
if (encryptedValue != null) {
EncryptDecryptField annotation = field.getAnnotation(EncryptDecryptField.class);
Class<?> value = annotation.value();
Method method = value.getMethod(annotation.decrypt(), String.class);
Object invoke = method.invoke(value, encryptedValue);
// String decryptedValue = ArithmeticAPI.base64Decod(encryptedValue);
field.set(o, invoke);
} else {
field.set(o, null);
}
} catch (IllegalAccessException e) {
// 處理異常
log.error(e.getMessage(), e);
}
}
}
}
}
return invocation.proceed();
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java數(shù)據(jù)結(jié)構之雙端鏈表原理與實現(xiàn)方法
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構之雙端鏈表原理與實現(xiàn)方法,簡單描述了雙端鏈表的概念、原理并結(jié)合實例形式分析了java實現(xiàn)雙端鏈表的相關操作技巧,需要的朋友可以參考下2017-10-10
Java實現(xiàn)求小于n的質(zhì)數(shù)的3種方法
這篇文章主要介紹了Java實現(xiàn)求小于n的質(zhì)數(shù)的3種方法,本文給出了根據(jù)定義去求解、平方根、找規(guī)律三種解法,需要的朋友可以參考下2015-03-03

