目录
查询数据
前言
在之前的文章中我们实现了springboot整合mybits-plus,并且配置了多数据源,springboot配置多数据源-CSDN博客。接下来我们来实现一下敏感数据的保存并查询。
添加依赖
<!--AES加密解密需要包-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
代码
handle类
package com.lyy.demo5.handler;
import com.lyy.demo5.utils.AesUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.*;
/**
* @author lyy
*/
public class TypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, AesUtil.encrypt(parameter));
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return AesUtil.decrypt(rs.getString(columnName));
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return AesUtil.decrypt(rs.getString(columnIndex));
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return AesUtil.decrypt(cs.getString(columnIndex));
}
}
加解密工具类
package com.lyy.demo5.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* aes 加密的工具类
* 1.存储 加密的秘钥key
* 2.实现 aes 加密
* 3.实现aes解密的功能
* @author lyy
*/
@Slf4j
public class AesUtil {
/**
* 定义 aes 加密的key
* 密钥 必须是16位, 自定义,
* 如果不是16位, 则会出现InvalidKeyException: Illegal key size
* 解决方案有两种:
* 需要安装Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files(可以在Oracle下载).
* .设置设置key的长度为16个字母和数字的字符窜(128 Bit/8=16字符)就不报错了。
*/
private static final String KEY = "KEYBYACSJAVAZXLL";
/**
* 偏移量
*/
private static final int OFFSET = 16;
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static final String ALGORITHM = "AES";
/**
* 加密
* @param content content
* @return String
*/
public static String encrypt(String content) {
return encrypt(content, KEY);
}
/**
* 解密
*
* @param content content
* @return String
*/
public static String decrypt(String content) {
return decrypt(content, KEY);
}
/**
* 加密
*
* @param content 需要加密的内容
* @param key 加密密码
* @return String
*/
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);
//定义加密编码
String charset = "utf-8";
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) {
log.debug("加密失败:{}",e.getMessage());
}
return null;
}
/**
* AES(256)解密
*
* @param content 待解密内容
* @param key 解密密钥
* @return 解密之后
*/
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);
// 初始化
String charset = "utf-8";
cipher.init(Cipher.DECRYPT_MODE, skey, iv);
byte[] result = cipher.doFinal(new Base64().decode(content));
// 解密
return new String(result,charset);
} catch (Exception e) {
log.debug("解密失败:{}",e.getMessage());
}
return null;
}
public static void main(String[] args) {
String encrypt = encrypt("15112345678");
System.out.println(encrypt);
}
}
实体类
package com.lyy.demo5.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.lyy.demo5.handler.TypeHandler;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@TableName(value = "tb_user",autoResultMap = true)
public class User extends Model<User> {
/**
* 主键
*/
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/**
* 用户名
*/
@TableField(typeHandler = TypeHandler.class)
private String username;
/**
* 用户手机号码
*/
private String phone;
}
加解密方法
方式一:
实体类字段上添加注解:@TableField(typeHandler = TypeHandler.class)、@TableName(value = "数据库表名", autoResultMap = true);
方式二:
如果mapper中是以resultMap接收,创建 ResultMap
映射,指定 typeHandler="TypeHandler的类路径"
,对于加密字段的查询只能在xml中进行配置{字段,typeHandler=com.lyy.demo3.handler.TypeHandler}
<?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.lyy.demo3.mapper.RolesMapper">
<resultMap id="rolesMap" type="com.lyy.demo3.entity.Roles">
<id property="id" column="id" />
<result property="username" column="username" typeHandler="com.lyy.demo3.handler.TypeHandler" />
<result property="role" column="role"/>
</resultMap>
<select id="queryRoleByName" resultMap="rolesMap">
select * from roles where username = #{username,typeHandler=com.lyy.demo3.handler.TypeHandler}
</select>
<select id="queryRolesAll" resultMap="rolesMap">
select * from roles
</select>
</mapper>