前言
当你的公司达到一定规模的时候,会有专门的人员来审计你们公司的数据,尤其是支付公司,财税公司,当审计人员发现你们的数据库中所有人的真实信息都是明文存储时,肯定是不可以的。本文介绍配合Mybatis-plus来实现数据脱敏,demo将会在文章最后附上地址。
基于TypeHandler数据库脱敏方案
由于项目数据库中间件使用的是Mybatis,所以使用Mybatis中的BaseTypeHandler的一个类型处理器,对数据进行AES加密存入数据。
代码实现
1 | import lombok.extern.slf4j.Slf4j; |
1 | import cn.hutool.core.text.CharSequenceUtil; |
Handler使用
1 |
|
单元测试
1 | import org.junit.Test; |
数据库结果
mobile | id_card |
---|---|
7ylDd+J1hwZob5fKaa2ZgQ== | I6zf0lL2jmul0NTb0QE0htbGc9jNEezjU0On9vKHhng= |
由此看出我们数据库里虽然存的是密文但是在单元测试中可以和明文匹配,由此数据库脱敏也就完成了。
【转载】 基于Mybatis插件+注解实现方案
1 | public interface Crypt { |
1 | package xyz.molzhao.crypt; |
1 | package xyz.molzhao.crypt; |
1 | package xyz.molzhao.crypt; |
1 | package xyz.molzhao.crypt; |
1 | package xyz.molzhao.crypt; |
1 | package xyz.molzhao.crypt; |
1 | package xyz.molzhao.crypt; |
添加Mybatis插件
1 |
|
1 |
|
1 | public interface UserMapper extends BaseMapper<User> { |
使用的时候只需要在变量上加上@CryptField
注解即可。