如何通過一個注解實現(xiàn)MyBatis字段加解密
簡介
mybatis-crypto 是一個基于 mybatis 插件機制實現(xiàn)的字段加解密組件,通過一個注解即可對敏感數(shù)據(jù)進行加解密處理。 支持自定義 Encryptor、特殊字段單獨指定 Encryptor 和 key ,滿足大部分使用場景。
模塊
mybatis-crypto 包括三個模塊:
- mybatis-crypto-core 插件的核心功能模塊
- mybatis-crypto-spring-boot-starter 提供了 Spring boot 快速整合功能
- mybatis-crypto-encryptors 提供了一些 IEncryptor 實現(xiàn)
使用方法
引入依賴
<dependency> <groupId>io.github.whitedg</groupId> <artifactId>mybatis-crypto-spring-boot-starter</artifactId> <version>${latest.version}</version> </dependency>
實現(xiàn) IEncryptor
import io.github.whitedg.mybatis.crypto.IEncryptor; public class MyEncryptor implements IEncryptor { @Override public String encrypt(Object val2bEncrypted, String key) throws Exception { // 實現(xiàn)這個方法返回加密后的數(shù)據(jù) return "encrypted string"; } @Override public String decrypt(Object val2bDecrypted, String key) throws Exception { // 實現(xiàn)這個方法返回解密后的數(shù)據(jù) return "decrypted string"; } }
或者引入 mybatis-crypto-encryptors
<dependency> <groupId>io.github.whitedg</groupId> <artifactId>mybatis-crypto-encryptors</artifactId> <version>${latest.version}</version> </dependency>
使用其提供的 Encryptor:
- io.github.whitedg.mybatis.crypto.Base64Encryptor
- io.github.whitedg.mybatis.crypto.BasicTextEncryptor
- io.github.whitedg.mybatis.crypto.AES256Encryptor
- io.github.whitedg.mybatis.crypto.StrongTextEncryptor
添加配置
mybatis-crypto: # 是否啟用插件,默認 true enabled: true # 快速失敗,默認 true fail-fast: false # 全局默認 Encryptor default-encryptor: io.github.whitedg.mybatis.crypto.BasicTextEncryptor # Encryptor 默認密鑰 default-key: global-key # mybatis @Param 注解下需要加解密的參數(shù) key 前綴 mapped-key-prefixes: et,encrypted
指定加密字段
- 在需要加解密的字段上添加注解 @EncryptedField
public class User { @EncryptedField private String encryptedStr; @EncryptedField(encryptor = YourEncryptor.class, key = "Your Key") private String customizedStr; }
- 使用配置的 @Param 參數(shù) key 前綴
import org.apache.ibatis.annotations.Param; interface YourEntityMapper { int insert(@Param("et") YourEntity entity); // 支持數(shù)組 int batchInsert(@Param("encrypted-entities") List<YourEntity> entity); // 返回值也支持單個對象或數(shù)組 YourEntity selectOne(); List<YourEntity> selectList(); }
配置項說明
配置項 | 說明 | 默認值 |
---|---|---|
mybatis-crypto.enabled | 是否啟用 mybatis-crypto | true |
mybatis-crypto.fail-fast | 快速失敗,加解密過程中發(fā)生異常是否中斷。true:拋出異常,false:使用原始值,打印 warn 級別日志 | true |
mybatis-crypto.mapped-key-prefixes | @Param 參數(shù)名的前綴,前綴匹配則會進行加密處理 | 空 |
mybatis-crypto.default-encryptor | 全局默認 Encryptor | 空 |
mybatis-crypto.default-key | 全局默認 Encryptor 的密鑰 | 空 |
開源鏈接
總結(jié)
到此這篇關(guān)于如何通過一個注解實現(xiàn)MyBatis字段加解密的文章就介紹到這了,更多相關(guān)注解實現(xiàn)MyBatis字段加解密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java中while和do-while循環(huán)、break的使用
本文介紹了循環(huán)結(jié)構(gòu)語句while和do-while循環(huán)、break的使用,while循環(huán)語句通過流程圖和語法語句結(jié)合一個求1~10的整數(shù)和的例子來幫助大家理解while循環(huán)的用法,感興趣的朋友跟隨小編來看看吧2020-11-11springMVC框架下JQuery傳遞并解析Json數(shù)據(jù)
json作為一種輕量級的數(shù)據(jù)交換格式,在前后臺數(shù)據(jù)交換中占據(jù)著非常重要的地位,這篇文章主要介紹了springMVC框架下JQuery傳遞并解析Json數(shù)據(jù),有興趣的可以了解一下。2017-01-01Java8中新特性O(shè)ptional、接口中默認方法和靜態(tài)方法詳解
Java 8 已經(jīng)發(fā)布很久了,很多報道表明Java 8 是一次重大的版本升級。下面這篇文章主要給大家介紹了關(guān)于Java8中新特性O(shè)ptional、接口中默認方法和靜態(tài)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。2017-12-12Servlet和Filter之間的區(qū)別與聯(lián)系
這篇文章主要介紹了Servlet和Filter之間的區(qū)別與聯(lián)系的相關(guān)資料,需要的朋友可以參考下2016-05-05spring boot實現(xiàn)超輕量級網(wǎng)關(guān)的方法(反向代理、轉(zhuǎn)發(fā))
這篇文章主要介紹了spring boot實現(xiàn)超輕量級網(wǎng)關(guān)(反向代理、轉(zhuǎn)發(fā))的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11