Mybatis的TypeHandler加解密數(shù)據(jù)實(shí)現(xiàn)
一、背景
在我們數(shù)據(jù)庫(kù)中有些時(shí)候會(huì)保存一些用戶(hù)的敏感信息,比如: 手機(jī)號(hào)、銀行卡等信息,如果這些信息以明文的方式保存,那么是不安全的。假如: 黑客黑進(jìn)了數(shù)據(jù)庫(kù),或者離職人員導(dǎo)出了數(shù)據(jù),那么就可能導(dǎo)致這些敏感數(shù)據(jù)的泄漏。因此我們就需要找到一種方法來(lái)解決這個(gè)問(wèn)題。
二、解決方案
由于我們系統(tǒng)中使用了Mybatis作為數(shù)據(jù)庫(kù)持久層,因此決定使用Mybatis的TypeHandler或Plugin來(lái)解決。
TypeHandler : 需要我們?cè)谀承┝猩鲜謩?dòng)指定 typeHandler 來(lái)選擇使用那個(gè)typeHandler或者根據(jù)@MappedJdbcTypes 和 @MappedTypes注解來(lái)自行推斷。
<result column="phone" property="phone" typeHandler="com.huan.study.mybatis.typehandler.EncryptTypeHandler"/>
Plugin : 可以攔截系統(tǒng)中的 select、insert、update、delete等語(yǔ)句,也能獲取到sql執(zhí)行前的參數(shù)和執(zhí)行后的數(shù)據(jù)。
經(jīng)過(guò)考慮,決定使用TypeHandler來(lái)加解密數(shù)據(jù)。
三、需求
我們有一張客戶(hù)表customer,里面有客戶(hù)手機(jī)號(hào)(phone)和客戶(hù)地址(address)等字段,其中客戶(hù)手機(jī)號(hào)(phone)是需要加密保存到數(shù)據(jù)庫(kù)中的。
1、在添加客戶(hù)信息時(shí),自動(dòng)將客戶(hù)手機(jī)號(hào)加密保存到數(shù)據(jù)中。
2、在查詢(xún)客戶(hù)信息時(shí),自動(dòng)解密客戶(hù)手機(jī)號(hào)。
四、實(shí)現(xiàn)思路
1、編寫(xiě)一個(gè)實(shí)體類(lèi),凡是此實(shí)體類(lèi)的數(shù)據(jù)都表示需要加解密的
public class Encrypt {
private String value;
public Encrypt() {
}
public Encrypt(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
2、編寫(xiě)一個(gè)加解密的TypeHandler
- 設(shè)置參數(shù)時(shí),加密數(shù)據(jù)。
- 從數(shù)據(jù)庫(kù)獲取記錄時(shí),解密數(shù)據(jù)。
package com.huan.study.mybatis.typehandler;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 加解密TypeHandler
*
* @author huan.fu 2021/5/18 - 上午9:20
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Encrypt.class)
public class EncryptTypeHandler extends BaseTypeHandler<Encrypt> {
private static final byte[] KEYS = "12345678abcdefgh".getBytes(StandardCharsets.UTF_8);
/**
* 設(shè)置參數(shù)
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Encrypt parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null || parameter.getValue() == null) {
ps.setString(i, null);
return;
}
AES aes = SecureUtil.aes(KEYS);
String encrypt = aes.encryptHex(parameter.getValue());
ps.setString(i, encrypt);
}
/**
* 獲取值
*/
@Override
public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
return decrypt(rs.getString(columnName));
}
/**
* 獲取值
*/
@Override
public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return decrypt(rs.getString(columnIndex));
}
/**
* 獲取值
*/
@Override
public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return decrypt(cs.getString(columnIndex));
}
public Encrypt decrypt(String value) {
if (null == value) {
return null;
}
return new Encrypt(SecureUtil.aes(KEYS).decryptStr(value));
}
}
注意⚠️:
- @MappedTypes:表示該處理器處理的java類(lèi)型是什么。
- @MappedJdbcTypes:表示處理器處理的Jdbc類(lèi)型。
3、sql語(yǔ)句中寫(xiě)法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.huan.study.mybatis.mappers.CustomerMapper">
<resultMap id="BaseResultMapper" type="com.huan.study.mybatis.entity.Customer">
<id column="id" property="id"/>
<result column="phone" property="phone"/>
<result column="address" property="address"/>
</resultMap>
<insert id="addCustomer">
insert into customer(phone,address) values (#{phone},#{address})
</insert>
<select id="findCustomer" resultMap="BaseResultMapper">
select * from customer where phone = #{phone}
</select>
</mapper>
SQL中沒(méi)有什么特殊的寫(xiě)法。
4、配置文件中指定Typehandler的包路徑
mybatis.type-handlers-package=com.huan.study.mybatis.typehandler
5、編寫(xiě)后臺(tái)代碼
- 提供一個(gè)添加方法
- 提供一個(gè)根據(jù)手機(jī)號(hào)查詢(xún)的方法
后臺(tái)代碼比較簡(jiǎn)單,直接查看 https://gitee.com/huan1993/spring-cloud-parent/tree/master/mybatis/mybatis-typehandler-encrypt
貼一個(gè)mapper層的截圖。

mapper層的寫(xiě)法
6、測(cè)試結(jié)果

數(shù)據(jù)庫(kù)字段加解密結(jié)果
從測(cè)試結(jié)果中可知,添加數(shù)據(jù)時(shí),需要加密的數(shù)據(jù)(phone)在數(shù)據(jù)庫(kù)中已經(jīng)加密了,在查詢(xún)的時(shí)候,加密的數(shù)據(jù)已經(jīng)自動(dòng)解密了。
五、實(shí)現(xiàn)代碼
后臺(tái)代碼: https://gitee.com/huan1993/spring-cloud-parent/tree/master/mybatis/mybatis-typehandler-encrypt
六、參考文檔
1、https://mybatis.org/mybatis-3/zh/configuration.html#typeHandlers
2、https://github.com/mybatis/spring-boot-starter
到此這篇關(guān)于Mybatis的TypeHandler加解密數(shù)據(jù)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis TypeHandler加解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目
這篇文章主要介紹了如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目,幫助大家更好的利用springboot進(jìn)行網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-10-10
Java動(dòng)態(tài)線(xiàn)程池插件dynamic-tp集成zookeeper
ZooKeeper是一個(gè)分布式的,開(kāi)放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開(kāi)源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等2023-03-03
海量數(shù)據(jù)去重排序bitmap(位圖法)在java中實(shí)現(xiàn)的兩種方法
今天小編就為大家分享一篇關(guān)于海量數(shù)據(jù)去重排序bitmap(位圖法)在java中實(shí)現(xiàn)的兩種方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
解析Java多線(xiàn)程之常見(jiàn)鎖策略與CAS中的ABA問(wèn)題
本篇文章介紹了常見(jiàn)的鎖策略,并說(shuō)明了synchronized關(guān)鍵字加的鎖類(lèi)型不是單一一種鎖類(lèi)型的,根據(jù)可重入鎖與非可重入鎖引出了死鎖的概念與死鎖條件,最后介紹了CAS指令以及CAS鎖產(chǎn)生的ABA問(wèn)題及其解決方案,需要的朋友可以參考下2022-06-06
Java實(shí)現(xiàn)根據(jù)模板自動(dòng)生成新的PPT
這篇文章主要介紹了如何利用Java代碼自動(dòng)生成PPT,具體就是查詢(xún)數(shù)據(jù)庫(kù)數(shù)據(jù),然后根據(jù)模板文件(PPT),將數(shù)據(jù)庫(kù)數(shù)據(jù)與模板文件(PPT),進(jìn)行組合一下,生成新的PPT文件。感興趣的可以了解一下2022-02-02
IntelliJ IDEA 2020.2正式發(fā)布,兩點(diǎn)多多總能助你提效
這篇文章主要介紹了IntelliJ IDEA 2020.2正式發(fā)布,諸多亮點(diǎn)總有幾款能助你提效,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-07-07
Java:DocumentBuilderFactory調(diào)用XML的方法實(shí)例
Java:DocumentBuilderFactory調(diào)用XML的方法實(shí)例,需要的朋友可以參考一下2013-04-04
MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式
這篇文章主要介紹了MyBatis的@SelectProvider注解構(gòu)建動(dòng)態(tài)SQL方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

