MyBatis數(shù)據(jù)脫敏的實現(xiàn)方案介紹
一、背景
假如:黑客黑進了數(shù)據(jù)庫,或者離職人員導出了數(shù)據(jù),那么就可能導致這些敏感數(shù)據(jù)的泄漏。因此我們就需要找到一種方法來解決這個問題。
二、解決方案
由于我們系統(tǒng)中使用了Mybatis作為數(shù)據(jù)庫持久層,因此決定使用Mybatis的TypeHandler或Plugin來解決。
TypeHandler : 需要我們在某些列上手動指定 typeHandler 來選擇使用那個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,里面有客戶手機號(phone)和郵箱(email)等字段,其中客戶手機號(phone)是需要加密保存到數(shù)據(jù)庫中的。
1、 在添加用戶信息時,自動將用戶手機號加密保存到數(shù)據(jù)中;
2、 在查詢用戶信息時,自動解密用戶手機號;
四、實現(xiàn)思路
1、 編寫一個實體類,凡是此實體類的數(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、編寫一個加解密的TypeHandler
- 設置參數(shù)時,加密數(shù)據(jù)。
- 從數(shù)據(jù)庫獲取記錄時,解密數(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ù) */ @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依賴,對KEYS進行AES加密得到密鑰 AES aes = SecureUtil.aes(KEYS); //對Encrypt 類型的參數(shù)進行加密 String encrypt = aes.encryptHex(parameter.getValue()); ps.setString(i, encrypt); } /** * 獲取值 */ @Override public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException { //在獲取值是對加密的數(shù)據(jù)進行解密 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語句中寫法
測試方法
@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>
五、測試結果
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
這里為了不影響實體類字段的類型,用vo來進行映射
@Data @AllArgsConstructor @NoArgsConstructor public class UserVo { private Long id; private String name; private String nickname; private Integer age; //注意:針對需要解密的字段需要指定我們之前進行加密的類型,這樣才能匹配到我們自定義的TypeHandler private Encrypt phone; private Encrypt email; private String password; }
到此這篇關于MyBatis數(shù)據(jù)脫敏的實現(xiàn)方案介紹的文章就介紹到這了,更多相關MyBatis數(shù)據(jù)脫敏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Java中Checked Exception與Runtime Exception 的區(qū)別
這篇文章主要介紹了詳解Java中Checked Exception與Runtime Exception 的區(qū)別的相關資料,這里提供實例幫助大家學習理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08解決springSecurity 使用默認登陸界面登錄后無法跳轉問題
這篇文章主要介紹了解決springSecurity 使用默認登陸界面登錄后無法跳轉問題,項目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-12-12mybatis-plus指定字段模糊查詢的實現(xiàn)方法
最近項目中使用springboot+mybatis-plus來實現(xiàn),所以下面這篇文章主要給大家介紹了關于mybatis-plus實現(xiàn)指定字段模糊查詢的相關資料,需要的朋友可以參考下2022-04-04Intellij IDEA 最全超實用快捷鍵整理(長期更新)
這篇文章主要介紹了Intellij IDEA 最全實用快捷鍵整理(長期更新),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02如何使用Spring Validation優(yōu)雅地校驗參數(shù)
這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗參數(shù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07