mybatis攔截器自動(dòng)加密解密教程
mybatis攔截器自動(dòng)加密解密
來(lái)看實(shí)例
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) 遍歷父類(lèi)
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
// 如果屬性帶有EncryptField注解放到要加解密的集合中
if (field.isAnnotationPresent(EncryptDecryptField.class)) {
field.setAccessible(true);
try {
// 假設(shè)你有某種方法獲取加密值(根據(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) 遍歷父類(lèi)
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
// 如果屬性帶有EncryptField注解放到要加解密的集合中
if (field.isAnnotationPresent(EncryptDecryptField.class)) {
field.setAccessible(true);
try {
// 假設(shè)你有某種方法獲取加密值(根據(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中如何編寫(xiě)一個(gè)數(shù)的n次方(冪運(yùn)算)?
本文介紹了使用pow函數(shù)和自定義for循環(huán)計(jì)算冪的O(n)時(shí)間復(fù)雜度方法,然后重點(diǎn)講解了快速冪算法的分治思想,以及從二進(jìn)制角度的解釋,包括如何通過(guò)位運(yùn)算和循環(huán)迭代實(shí)現(xiàn)高效計(jì)算,給出了Java代碼實(shí)現(xiàn)2024-07-07
Java實(shí)現(xiàn)字符串轉(zhuǎn)換成可執(zhí)行代碼的方法
今天小編就為大家分享一篇Java實(shí)現(xiàn)字符串轉(zhuǎn)換成可執(zhí)行代碼的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法
這篇文章主要介紹了超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
java注解的類(lèi)型知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java注解的類(lèi)型知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03
一文詳解Spring中ResponseEntity包裝器的使用
在?Spring?中,ResponseEntity?是?HTTP?響應(yīng)的包裝器,這篇文章主要為大家詳細(xì)介紹了ResponseEntity包裝器的使用,感興趣的可以了解一下2025-02-02
基于FLink實(shí)現(xiàn)實(shí)時(shí)安全檢測(cè)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于FLink實(shí)現(xiàn)實(shí)時(shí)安全檢測(cè)的功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2023-02-02
SpringBoot中整合Ehcache實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存的詳細(xì)過(guò)程
這篇文章主要介紹了SpringBoot中整合Ehcache實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存,SpringBoot 中使用 Ehcache 比較簡(jiǎn)單,只需要簡(jiǎn)單配置,說(shuō)白了還是 Spring Cache 的用法,合理使用緩存機(jī)制,可以很好地提高項(xiàng)目的響應(yīng)速度,需要的朋友可以參考下2023-04-04
Java interrupt()方法使用注意_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java interrupt()方法使用注意_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-05-05

