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敏感字段加解密内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
使用 Java 将 byte[] 转换为 File 对象并上
本文展示了如何通过 Java 和 Spring 来处理图像文件的获取、保存和上传,通过 RestTemplate 获取字节数组并将其转换为 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


最新评论