Mybatis給數據庫敏感字段加解密詳解
前言
為了保護數據庫敏感字段數據安全,有時候我們需要將敏感數據加密入庫,查詢時再解密成明文。
我們可以利用Mybatis自定義TypeHandler來處理,下面我們來具體實現一下。
定義KeyCenterUtils加解密工具類
import org.springframework.stereotype.Service;
import java.util.Base64;
@Service
public class KeyCenterUtils {
public String encrypt(String src) {
try {
String result = Base64.getEncoder().encodeToString(src.getBytes("UTF-8"));
return result;
} catch (Exception e) {
throw new RuntimeException("encrypt fail!", e);
}
}
public String decrypt(String src) {
try {
byte[] asBytes = Base64.getDecoder().decode(src);
String result = new String(asBytes, "UTF-8");
return result;
} catch (Exception e) {
throw new RuntimeException("decrypt fail!", e);
}
}
}自定義Handler類實現數據庫字段加解密
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mk.util.KeyCenterUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CustomTypeHandler<T> extends BaseTypeHandler<T> {
@Autowired
private KeyCenterUtils keyCenterUtils;
public CustomTypeHandler() {
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, this.keyCenterUtils.encrypt((String)parameter));
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
String columnValue = rs.getString(columnName);
//有一些可能是空字符
return StringUtils.isBlank(columnValue) ? (T)columnValue : (T)this.keyCenterUtils.decrypt(columnValue);
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String columnValue = rs.getString(columnIndex);
return StringUtils.isBlank(columnValue) ? (T)columnValue : (T)this.keyCenterUtils.decrypt(columnValue);
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String columnValue = cs.getString(columnIndex);
return StringUtils.isBlank(columnValue) ? (T)columnValue : (T)this.keyCenterUtils.decrypt(columnValue);
}
}因為我用的是Mybatis-Plus,所以可以使用Mybatis-Plus的@TableField的注解通過typeHandler屬性指定上面自定義的Handler即可。
實體類添加注解
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "lemon_user", autoResultMap = true)
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
@TableField(typeHandler = CustomTypeHandler.class)
private String password;
private String salt;
}注意:上面的@TableName注解設置了autoResultMap = true的屬性值,這樣通過Mybatis-Plus的BaseMapper查詢出來的數據才會將加密字段進行解密,默認不生效。
如果不是Mybatis-Plus的 BaseMapper內部的方法,則需要我們在查詢時在resultMap的屬性中指定我們自定義的typeHandler,如下:
<resultMap id="baseResultMap" type="com.mk.entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password" typeHandler="com.mk.handler.CustomTypeHandler"/>
</resultMap>
<select id="getUserByName" resultMap="baseResultMap">
select * from lemon_user where username = #{username}
</select>到此這篇關于Mybatis給數據庫敏感字段加解密詳解的文章就介紹到這了,更多相關Mybatis敏感字段加解密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用 Java 將 byte[] 轉換為 File 對象并上
本文展示了如何通過 Java 和 Spring 來處理圖像文件的獲取、保存和上傳,通過 RestTemplate 獲取字節(jié)數組并將其轉換為 File 對象,可以輕松實現從遠程 URL 獲取文件并將其上傳到外部服務器,感興趣的朋友一起看看吧2025-03-03
springboot集成mybatis-plus遇到的問題及解決方法
這篇文章主要介紹了springboot集成mybatis-plus遇到的問題及解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Java中的java.lang.reflect.Type簡介
在 Java 中,java.lang.reflect.Type 是一個接口,代表所有類型的通用超類型,它包括原始類型、參數化類型、數組類型、類型變量和基本類型,本文給大家講解Java中的java.lang.reflect.Type是什么,需要的朋友可以參考下2024-06-06
解決MyBatis報錯:There is no getter for
這篇文章主要介紹了解決MyBatis報錯:There is no getter for property named'Xxx'in'class xxx.xxx.Xxx'問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
springcloud微服務基于redis集群的單點登錄實現解析
這篇文章主要介紹了springcloud微服務基于redis集群的單點登錄實現解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09

