mybatis插件優(yōu)雅實現(xiàn)字段加密的示例代碼
在很多時候,我們都需要字段加密。比如郵箱,密碼,電話號碼等。通常的項目可能會在插入數(shù)據(jù)前手動加密,今天給大家?guī)硪粋€更優(yōu)雅的做法——通過注解實現(xiàn)。
mybatis的接入點
Mybatis本質(zhì)上是對JDBC執(zhí)行流程的封裝。簡要概括下Mybatis這幾個可被代理類的職能。
- 【**Executor**】: 真正執(zhí)行SQL語句的對象,調(diào)用sqlSession的方法時,本質(zhì)上都是調(diào)用executor的方法,還負(fù)責(zé)獲取connection,創(chuàng)建StatementHandler。
- 【**StatementHandler**】: 創(chuàng)建并持有ParameterHandler和ResultSetHandler對象,操作JDBC的statement與進(jìn)行數(shù)據(jù)庫操作。
- 【**ParameterHandler**】: 處理入?yún)?,將Java方法上的參數(shù)設(shè)置到被執(zhí)行語句中。
- 【**ResultSetHandler**】: 處理SQL語句的執(zhí)行結(jié)果,將返回值轉(zhuǎn)換為Java對象。
如何接入自己寫的插件
@Intercepts({@Signature(type = 代理的插件類, method = 類中的方法名, args = {方法參數(shù)})}) public class MyInterceptor implements Interceptor { Object intercept(Invocation invocation) throws Throwable{ //invocation就是你要代理的方法。 //在這里寫主要的邏輯操作 //todo } default Object plugin(Object target) { //是用來生成代理的 //一般不重寫 return Plugin.wrap(target, this); } default void setProperties(Properties properties) { // NOP } }
talk is cheap,show me the code
加密
@Intercepts({@Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class})}) public class MybatisEncryptInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { return invocation; } @Override public Object plugin(Object target) { if (target instanceof ParameterHandler) { ParameterHandler parameterHandler = (ParameterHandler) target; Object parameterObject = parameterHandler.getParameterObject(); // 進(jìn)行加密操作 } return target; } @Override public void setProperties(Properties properties) { } }
選擇了在ParameterHandler#setParameters方法進(jìn)行加密。這時就有人會問了為什么不在intercept方法改變參數(shù)呢?
我們看一下Invocation類
public class Invocation { private final Object target; private final Method method; private final Object[] args; ... }
我們注意到三個變量都是用fianl修飾的,我們不能改變他們的值。所以我們在plugin方法進(jìn)行加密操作。
解密
有加密就有解密
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})}) public class MybatisDecryptInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { // 獲取執(zhí)行mysql執(zhí)行結(jié)果 Object result = invocation.proceed(); if (result == null) { return null; } //解密result return result; } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
選擇了在ResultSetHandler#handleResultSets方法進(jìn)行加密,ibatis起名確實不錯,一看方法名就知道干什么了。
取消加解密
有些特定的場合我們不希望加解密,但是大部分時候又需要加解密,這種情況的話我們就用不了了嗎?難道只能手動編碼嗎qwq。
我以pagehelper為靈感寫了一個EncryptHelper。
讓我們看看核心邏輯
@Intercepts({@Signature(type = Executor.class,method = "flushStatements",args = {})}) public class MybatisClearInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { Object result = invocation.proceed(); if(EncryptHelper.hasSituation()){ log.info("EncryptClear!!!"); } EncryptHelper.clearEncrypt(); return result; } @Override public Object plugin(Object target) { // 包裝目標(biāo)對象,創(chuàng)建代理對象 return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
在這里我們每次執(zhí)行sql后就調(diào)用 EncryptHelper.clearEncrypt();方法。在EncryptHelper中存放ThreadLocal<EncryptSituation>,加解密時可以根據(jù)EncryptHelper中的LOCAL_ENCRYPT判斷是否加解密。在執(zhí)行完一次sql后自動清除。當(dāng)然你也可以手動管理不啟用這個ClearInterceptor。
public class EncryptHelper implements AutoCloseable { private static final ThreadLocal<EncryptSituation> LOCAL_ENCRYPT=new ThreadLocal<>(); //操作 }
總結(jié)
其中還有一些問題,比如MybatisClearInterceptor會和pagehelper不兼容因為分頁查詢前會查詢總數(shù)據(jù)量這里會有兩個sql,需要更加復(fù)雜的判斷。
有些同學(xué)會問為什么不在MybatisEncryptInterceptor和MybatisDecryptInterceptor里管理EncryptHelper,而是多寫一個MybatisClearInterceptor?有這種疑問的同學(xué)可以自己試一試,會發(fā)現(xiàn)神奇的事情,我也是試了好久。
到此這篇關(guān)于mybatis插件優(yōu)雅實現(xiàn)字段加密的示例代碼的文章就介紹到這了,更多相關(guān)mybatis 字段加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java SpringBoot+vue+實戰(zhàn)項目詳解
這篇文章主要介紹了SpringBoot+VUE實現(xiàn)前后端分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09Java代理模式實例詳解【靜態(tài)代理與動態(tài)代理】
這篇文章主要介紹了Java代理模式,結(jié)合實例形式詳細(xì)分析了java靜態(tài)代理與動態(tài)代理模式相關(guān)概念、原理、操作技巧與注意事項,需要的朋友可以參考下2019-09-09