数据库中的敏感信息,比如身份证号,手机号等,在进行保存时可能需要我们进行加密保存,在读取时又需要我们进行明文展示。如果使用Mybatis来对数据进行持久化,有两种方式来实现这一目的。
一、代码示例
1.1 项目结构
1.2 代码示例
配置文件
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis-tutorial?useUnicode=true&characterEncoding=utf8&autoReconnectForPools=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=Passw0rd
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# typeHandler包路径
mybatis.type-handlers-package=com.ddmit.mybatis.typehandler
mybatis.mapper-locations=mappers/**.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30
# 输出日志
logging.level.com.ddmit.mybatis.mappers=trace
加密类型实体类
package com.ddmit.mybatis.typehandler;
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;
}
}
TypeHandler加解密处理
package com.ddmit.mybatis.typehandler;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 加解密TypeHandler
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Encrypt.class)
public class EncryptTypeHandler extends BaseTypeHandler<Encrypt> {
private static final byte[] KEYS = "12345678abcdefgh".getBytes(StandardCharsets.UTF_8);
/**
* 设置参数
*/
@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;
}
AES aes = SecureUtil.aes(KEYS);
String encrypt = aes.encryptHex(parameter.getValue());
ps.setString(i, encrypt);
}
/**
* 获取值
*/
@Override
public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
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));
}
/**
* 解密方法
*
* @param value 待解密字符串
* @return 解密结果
*/
public Encrypt decrypt(String value) {
if (null == value) {
return null;
}
return new Encrypt(SecureUtil.aes(KEYS).decryptStr(value));
}
}
数据库表对应实体类
package com.ddmit.mybatis.entity;
import com.ddmit.mybatis.typehandler.Encrypt;
public class Customer {
private Integer id;
/*需要加密/解密的字段,声明为加密类型实体类类型*/
private Encrypt phone;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Encrypt getPhone() {
return phone;
}
public void setPhone(Encrypt phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
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="com.ddmit.mybatis.mappers.CustomerMapper">
<resultMap id="BaseResultMapper" type="com.ddmit.mybatis.entity.Customer">
<id column="id" property="id"/>
<result column="phone" property="phone"/>
<result column="address" property="address"/>
</resultMap>
<insert id="addCustomer">
insert into customer(phone,address) values (#{phone},#{address})
</insert>
<select id="findCustomer" resultMap="BaseResultMapper">
select * from customer where phone = #{phone}
</select>
</mapper>
RestController
对使用到的加密字段进行处理。
package com.ddmit.mybatis.controller;
import com.ddmit.mybatis.entity.Customer;
import com.ddmit.mybatis.mappers.CustomerMapper;
import com.ddmit.mybatis.typehandler.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@Autowired
private CustomerMapper customerMapper;
@GetMapping("addCustomer")
public String addCustomer(@RequestParam("phone") String phone, @RequestParam("address") String address) {
int result = customerMapper.addCustomer(new Encrypt(phone), address);
return "添加结果: " + result;
}
@GetMapping("findCustomer")
public Customer findCustomer(@RequestParam("phone") String phone) {
return customerMapper.findCustomer(new Encrypt(phone));
}
}
二、完整代码
详见: Mybatis使用typeHandlers对数据库字段加密/解密。
参考:
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的