如何通過一個注解實現(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)文章
快速解決 MyBatis-Plus 中 ID 自增問題(推薦)
本文介紹了MyBatis-Plus中自動生成ID過長導致的問題及解決方法,結(jié)合示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2025-02-02
SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄
這篇文章主要介紹了SpringBoot和VUE源碼直接整合打包成jar的踩坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Spring?MVC DispatcherServlet處理請求過程示例詳解
這篇文章主要介紹了Spring?MVC?DispatcherServlet處理請求過程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
微信公眾號支付(二)實現(xiàn)統(tǒng)一下單接口
本篇文章主要給大家介紹調(diào)用微信公眾支付的統(tǒng)一下單API,通過參數(shù)封裝為xml格式并發(fā)送到微信給的接口地址就可以獲得返回內(nèi)容,需要的朋友可以參考下本文2015-09-09

