参考: https://blog.csdn.net/weixin_45906061/article/details/126175722
https://blog.csdn.net/qq_31289187/article/details/104761910
为防止盗库 数据库中存储的字段需要加密
demo项目结构
1. pom文件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
2. 实体类
@TableName(value = "wharf_device_del",autoResultMap = true)
// # autoResultMap = true 必须写 不然返回值不能解密
@Data
public class WharfDeviceDelDo extends BaseDo {
/**
* 码头id
*/
private Long wharfId;
/**
* 设备名称
* @TableField(typeHandler = EncryptHandler.class) 指定之后才能加密
*/
@TableField(typeHandler = EncryptHandler.class)
private String deviceName;
/**
* 设备ID
*/
private Long deviceId;
/**
* 设备类型
*/
private String deviceType;
}
Mapper.java
@Mapper
public interface WharfDeviceDelMapper extends BaseMapper<WharfDeviceDelDo> {
List<WharfDeviceDelVO> selectInfo();
void addWharfDeviceDel( List<WharfDeviceDelVO> wharfDeviceDelVOS);
}
Service.java
public interface WharfDeviceDelService extends IService<WharfDeviceDelDo> {
List<WharfDeviceDelVO> selectInfo();
void addWharfDeviceDel( List<WharfDeviceDelVO> wharfDeviceDelVOS);
}
ServiceImpl.java
@Service("wharfDeviceDelService")
public class WharfDeviceDelServiceImpl extends ServiceImpl<WharfDeviceDelMapper, WharfDeviceDelDo> implements WharfDeviceDelService {
@Override
public List<WharfDeviceDelVO> selectInfo(){
return this.baseMapper.selectInfo();
}
@Override
public void addWharfDeviceDel( List<WharfDeviceDelVO> wharfDeviceDelVOS) {
this.baseMapper.addWharfDeviceDel(wharfDeviceDelVOS);
}
}
VO.java
@Data
public class WharfDeviceDelVO {
private Long wharfId;
private String deviceName;
private String zxcName;
}
3. 继承BaseTypeHandler的实现类
@MappedJdbcTypes(JdbcType.VARCHAR)
public class EncryptHandler extends BaseTypeHandler<String> {
/**
* 线上运行后勿修改,会影响已加密数据解密
*/
private static final byte[] KEYS = "shc987654321camp".getBytes(StandardCharsets.UTF_8);
/**
* 设置参数
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
if (StringUtils.isEmpty(parameter)) {
ps.setString(i, null);
return;
}
AES aes = SecureUtil.aes(KEYS);
String encrypt = aes.encryptHex(parameter);
ps.setString(i, encrypt);
}
/**
* 获取值
*/
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return decrypt(rs.getString(columnName));
}
/**
* 获取值
*/
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return decrypt(rs.getString(columnIndex));
}
/**
* 获取值
*/
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return decrypt(cs.getString(columnIndex));
}
public String decrypt(String value) {
if (null == value) {
return null;
}
return SecureUtil.aes(KEYS).decryptStr(value);
}
}
4. Mapper.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.fuly.mybatis_plus.dao.mapper.WharfDeviceDelMapper">
<resultMap type="top.fuly.mybatis_plus.vo.WharfDeviceDelVO" id="PlanMap">
<result column="wharf_id" jdbcType="INTEGER" property="wharfId"></result>
<!--解密列 score -->
<result column="device_name" jdbcType="VARCHAR" property="deviceName" typeHandler="com.xxx.mybatis_plus.dbhandler.EncryptHandler"></result>
<result column="zxc_name" jdbcType="VARCHAR" property="zxcName"></result>
</resultMap>
<select id="selectInfo" resultMap="PlanMap">
select wd.wharf_id,wd.device_name,wf.zxc_name from wharf_device_del wd left join wharf_del wf on wd.wharf_id =wf.id
</select>
<insert id="addWharfDeviceDel" parameterType="top.fuly.mybatis_plus.vo.WharfDeviceDelVO">
insert into wharf_device_del (wharf_id,device_name) values
<foreach collection="list" item="item" separator=",">
<!--加密列 -->
(#{item.wharfId},#{item.deviceName,typeHandler=com.xxx.mybatis_plus.dbhandler.EncryptHandler})
</foreach>
</insert>
</mapper>
测试代码
@Test
public void demo3() {
// 不走mapper.xml的 加解密
{
WharfDeviceDelDo wharfDeviceDelDo = new WharfDeviceDelDo();
wharfDeviceDelDo.setWharfId(6222378L);
wharfDeviceDelDo.setDeviceId(1111L);
wharfDeviceDelDo.setDeviceName("张三00000李四");
wharfDeviceDelService.save(wharfDeviceDelDo);
List<WharfDeviceDelDo> list = wharfDeviceDelService.list();
System.out.println(list);
}
// 使用mapper.xml的 加解密
{
WharfDeviceDelVO wharfDeviceDelDo = new WharfDeviceDelVO();
wharfDeviceDelDo.setWharfId(123456789L);
wharfDeviceDelDo.setDeviceName("工作站");
List<WharfDeviceDelVO> wharfDeviceDelVOS1 = Arrays.asList(wharfDeviceDelDo);
wharfDeviceDelService.addWharfDeviceDel(wharfDeviceDelVOS1);
List<WharfDeviceDelVO> wharfDeviceDelVOS = wharfDeviceDelService.selectInfo();
System.out.println(wharfDeviceDelVOS);
}
}
数据库:
有些文章说 要指定 自定义类型包处理器路径
mybatis-plus.type-handlers-package: com.xxx.mybatis_plus.dbhandler
我加了 然后删了 效果一样
加密算法那块 可以换成加密强度更高的其他算法