MyBatis數(shù)據(jù)脫敏的實(shí)現(xiàn)方案介紹
一、背景
假如:黑客黑進(jìn)了數(shù)據(jù)庫,或者離職人員導(dǎo)出了數(shù)據(jù),那么就可能導(dǎo)致這些敏感數(shù)據(jù)的泄漏。因此我們就需要找到一種方法來解決這個(gè)問題。
二、解決方案
由于我們系統(tǒng)中使用了Mybatis作為數(shù)據(jù)庫持久層,因此決定使用Mybatis的TypeHandler或Plugin來解決。
TypeHandler : 需要我們?cè)谀承┝猩鲜謩?dòng)指定 typeHandler 來選擇使用那個(gè)typeHandler或者根據(jù)@MappedJdbcTypes 和 @MappedTypes注解來自行推斷。
<result column="phone" property="phone" typeHandler="com.llp.llpmybatis.typehandler.EncryptTypeHandler"/>
Plugin : 可以攔截系統(tǒng)中的 select、insert、update、delete等語句,也能獲取到sql執(zhí)行前的參數(shù)和執(zhí)行后的數(shù)據(jù)。
三、需求
我們有一張用戶表tbl_user,里面有客戶手機(jī)號(hào)(phone)和郵箱(email)等字段,其中客戶手機(jī)號(hào)(phone)是需要加密保存到數(shù)據(jù)庫中的。
1、 在添加用戶信息時(shí),自動(dòng)將用戶手機(jī)號(hào)加密保存到數(shù)據(jù)中;
2、 在查詢用戶信息時(shí),自動(dòng)解密用戶手機(jī)號(hào);
四、實(shí)現(xiàn)思路
1、 編寫一個(gè)實(shí)體類,凡是此實(shí)體類的數(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、編寫一個(gè)加解密的TypeHandler
- 設(shè)置參數(shù)時(shí),加密數(shù)據(jù)。
- 從數(shù)據(jù)庫獲取記錄時(shí),解密數(shù)據(jù)。
/**
* 注意??:
*
* @MappedTypes:表示該處理器處理的java類型是什么。
*
* @MappedJdbcTypes:表示處理器處理的Jdbc類型。
*/
@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;
}
//這里引入hutool依賴,對(duì)KEYS進(jìn)行AES加密得到密鑰
AES aes = SecureUtil.aes(KEYS);
//對(duì)Encrypt 類型的參數(shù)進(jìn)行加密
String encrypt = aes.encryptHex(parameter.getValue());
ps.setString(i, encrypt);
}
/**
* 獲取值
*/
@Override
public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
//在獲取值是對(duì)加密的數(shù)據(jù)進(jìn)行解密
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));
}
}3、配置文件中指定Typehandler的包路徑
mybatis.type-handlers-package=com.llp.llpmybatis.typehandler
#\u914D\u7F6Emybatis
mybatis:
configuration:
#\u5F00\u542F\u4E0B\u5212\u7EBF\u6620\u5C04\u9A7C\u5CF0\u547D\u540D
map-underscore-to-camel-case: true
#mybatis\u65E5\u5FD7\u6253\u5370
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# xml\u6587\u4EF6\u4F4D\u7F6E\u6620\u5C04
mapper-locations: classpath*:mapper/*.xml
type-handlers-package: com.llp.llpmybatis.typehandler
4、sql語句中寫法
測(cè)試方法
@Autowired
private TblUserDao userDao;
@Test
void contextLoads() {
Encrypt phone = new Encrypt("15023511828");
String name = "小紅";
Encrypt email = new Encrypt("xiaohong@123.com");
userDao.insertUser(name,phone,email);
}
userDao:需要加密的字段需要傳入我們前面指定的類型
void insertUser(@Param("name") String name,@Param("phone") Encrypt phone,@Param("email") Encrypt email);
sql寫法:照常寫就行,沒有什么特殊的地方
<insert id="insertUser">
insert into tbl_user (name,phone,email) values (#{name},#{phone},#{email})
</insert>

五、測(cè)試結(jié)果
controller
@RestController
public class MyTestController {
@Autowired
private TblUserDao tblUserDao;
@RequestMapping("/test")
public UserVo mytest(){
UserVo userVo = tblUserDao.findById(7L);
return userVo;
}
}dao
UserVo findById(@Param("id") Long id);
<resultMap id="userVo" type="com.llp.llpmybatis.vo.UserVo">
<result column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="nickname" jdbcType="VARCHAR" property="nickname" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="phone" property="phone" />
<result column="email" property="email" />
<result column="password" jdbcType="VARCHAR" property="password" />
</resultMap>
<select id="findById" resultMap="userVo">
select * from tbl_user where id = #{id}
</select>
vo
這里為了不影響實(shí)體類字段的類型,用vo來進(jìn)行映射
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserVo {
private Long id;
private String name;
private String nickname;
private Integer age;
//注意:針對(duì)需要解密的字段需要指定我們之前進(jìn)行加密的類型,這樣才能匹配到我們自定義的TypeHandler
private Encrypt phone;
private Encrypt email;
private String password;
}
到此這篇關(guān)于MyBatis數(shù)據(jù)脫敏的實(shí)現(xiàn)方案介紹的文章就介紹到這了,更多相關(guān)MyBatis數(shù)據(jù)脫敏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java中Checked Exception與Runtime Exception 的區(qū)別
這篇文章主要介紹了詳解Java中Checked Exception與Runtime Exception 的區(qū)別的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題
這篇文章主要介紹了解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題,項(xiàng)目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12
mybatis-plus指定字段模糊查詢的實(shí)現(xiàn)方法
最近項(xiàng)目中使用springboot+mybatis-plus來實(shí)現(xiàn),所以下面這篇文章主要給大家介紹了關(guān)于mybatis-plus實(shí)現(xiàn)指定字段模糊查詢的相關(guān)資料,需要的朋友可以參考下2022-04-04
Intellij IDEA 最全超實(shí)用快捷鍵整理(長期更新)
這篇文章主要介紹了Intellij IDEA 最全實(shí)用快捷鍵整理(長期更新),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
java 轉(zhuǎn)發(fā)和重定向區(qū)別及實(shí)例代碼
這篇文章主要介紹了java 轉(zhuǎn)發(fā)和重定向區(qū)別及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
異常點(diǎn)/離群點(diǎn)檢測(cè)算法——LOF解析
這篇文章主要介紹了異常點(diǎn)/離群點(diǎn)檢測(cè)算法——LOF解析,通過圖解文字描述的方式詳細(xì)的解析了該算法,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
如何使用Spring Validation優(yōu)雅地校驗(yàn)參數(shù)
這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗(yàn)參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

