Python 官方文档:入门教程 => 点击学习
目录mybatis-Plus 自动加密解密实现TypeHandler添加注解查询加密字段MyBatis-Plus 敏感数据的加密写加密解密的工具类继承BaseTypeHandler
通过使用MyBatis的typeHandler功能,对入参和出参进行处理,实现无缝加密解密(将明文加密后保存至数据库;从数据库读取时,自动将密文解密成明文)
@Slf4j
public class UserTypeHandler extends BaseTypeHandler<Object> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object parameter, JdbcType jdbcType) {
try {
if (StrUtil.isBlank((String) parameter)) {
return;
}
// todo 加密操作
String encrypt = "";
preparedStatement.setString(i, encrypt);
} catch (Exception e) {
log.error("typeHandler加密异常:" + e);
}
}
@Override
public Object getNullableResult(ResultSet resultSet, String columnName) throws sqlException {
String col = resultSet.getString(columnName);
try {
if (StrUtil.isBlank(col)) {
return col;
}
// todo 对结果col进行解密操作
return "";
} catch (Exception e) {
log.error("typeHandler解密异常:" + e);
}
return col;
}
@Override
public Object getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
String col = resultSet.getString(columnIndex);
try {
if (StrUtil.isBlank(col)) {
return col;
}
// todo 对结果col进行解密操作
return "";
} catch (Exception e) {
log.error("typeHandler解密异常:" + e);
}
return col;
}
@Override
public Object getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
String col = callableStatement.getString(columnIndex);
try {
if (StrUtil.isBlank(col)) {
return col;
}
// todo 对结果col进行解密操作
return "";
} catch (Exception e) {
log.error("typeHandler解密异常:" + e);
}
return col;
}
}
在对应的实体类中
@Data
@TableName(value = "t_user", autoResultMap = true)
public class UserEntity {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
@TableField(typeHandler = UserTypeHandler.class)
private String passWord;
private String role;
}
MyBatis-Plus的QueryWrapper在查询加密字段时,并不会进入TypeHandler,需要手写sql,指定字段进行TypeHandler中的流程。
注解方式
@Results(id= "resultMap", value = {
@Result(column = "password", property = "password", typeHandler = UserTypeHandler.class)
})
@Select("select * from t_user where password = #{password, typeHandler=com.mybatisdemo.config.UserTypeHandler}")
List<UserEntity> list(UserQuery userQuery);
XML方式
<resultMap id="userMap" type="com.mybatisdemo.entity.UserEntity">
<result column="password" property="password" typeHandler="com.mybatisdemo.config.UserTypeHandler" />
</resultMap>
<select id="list" resultMap="userMap">
select * from t_user where password = #{password,typeHandler=com.mybatisdemo.config.UserTypeHandler}
</select>
最近在做项目,需要实现对身份证,密码等敏感数据的加密,即不能以明文存储密码到数据库。
上网查了一下资料,解决办法如下:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecreTKEySpec;
public class AES {
// 密钥
public static String key = "AD42F6697B035B7580E4FEF93BE20BAD";
private static String charset = "utf-8";
// 偏移量
private static int offset = 16;
private static String transfORMation = "AES/CBC/PKCS5Padding";
private static String alGorithm = "AES";
public static String encrypt(String content) {
return encrypt(content, key);
}
public static String decrypt(String content) {
return decrypt(content, key);
}
public static String encrypt(String content, String key) {
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
Cipher cipher = Cipher.getInstance(transformation);
byte[] byteContent = content.getBytes(charset);
cipher.init(Cipher.ENCRYPT_MODE, skey, iv);// 初始化
byte[] result = cipher.doFinal(byteContent);
return new Base64().encodeToString(result); // 加密
} catch (Exception e) {
// LogUtil.exception(e);
}
return null;
}
public static String decrypt(String content, String key) {
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化
byte[] result = cipher.doFinal(new Base64().decode(content));
return new String(result); // 解密
} catch (Exception e) {
//LogUtil.exception(e);
}
return null;
}
public static void main(String[] args) throws Exception {
String s = "hello world";
// 加密
System.out.println("加密前:" + s);
String encryptResultStr = encrypt(s);
System.out.println("加密后:" + encryptResultStr);
// 解密
System.out.println("解密后:" + decrypt(encryptResultStr));
}
}
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AESEncryptHandler extends BaseTypeHandler {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, AES.encrypt((String)parameter));
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String columnValue = rs.getString(columnName);
return AES.decrypt(columnValue);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String columnValue = rs.getString(columnIndex);
return AES.decrypt(columnValue);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
String columnValue = cs.getString(columnIndex);
return AES.decrypt(columnValue);
}
}
@Data
@EqualsAndHashCode(callSuper = false)
@TableName(autoResultMap = true)
public class SysOrgUser extends BaseUpdateModel {
private String loginName;
@TableField(typeHandler = AESEncryptHandler.class)
private String password;
至此,密码等敏感信息已处理好。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: MyBatis-Plus如何实现自动加密解密
本文链接: https://lsjlt.com/news/136438.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0