springboot数据库敏感数据加密解密

springboot数据库敏感数据加密解密_第1张图片

低价云服务器   链接—>>> 开发云 - 一站式云服务平台


 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/a1053765496/article/details/120484312

springboot数据库敏感数据加密解密_第2张图片

先上效果图:

加密前

springboot数据库敏感数据加密解密_第3张图片

 加密后

springboot数据库敏感数据加密解密_第4张图片

开始

import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 加解密
 *
 * @author lixx
 * @version 1.0
 * @since 2021-08-26 16:02
 */
public class Encrypt {

	/**
	 * 加密
	 */
	public static String encrypt(String src) {
		if (StringUtils.isBlank(src))
			return null;
		try {
			return Base64.getEncoder().encodeToString(src.getBytes(StandardCharsets.UTF_8));
		} catch (Exception e) {
			throw new RuntimeException("encrypt fail!", e);
		}
	}

	/**
	 * 解密
	 */
	public static String decrypt(String src) {
		if (StringUtils.isBlank(src))
			return null;
		try {
			byte[] asBytes = Base64.getDecoder().decode(src);
			return new String(asBytes, StandardCharsets.UTF_8);
		} catch (Exception e) {
			throw new RuntimeException("decrypt fail!", e);
		}
	}

}
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;

/**
 * 加解密
 *
 * @author lixx
 * @version 1.0
 * @since 2021-08-26 16:13
 */
public class EncryptHandler extends BaseTypeHandler {
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
		ps.setString(i, Encrypt.encrypt((String) parameter));
	}

	@Override
	public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
		String columnValue = rs.getString(columnName);
		return Encrypt.decrypt(columnValue);
	}

	@Override
	public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		String columnValue = rs.getString(columnIndex);
		return Encrypt.decrypt(columnValue);
	}

	@Override
	public String getNullableResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		String columnValue = cs.getString(columnIndex);
		return Encrypt.decrypt(columnValue);
	}
}

加解密:

使用如下:

方式一:返回的是实体类

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName(autoResultMap = true)    // 和数据库映射的实体类需要加autoResultMap 这个属性,加解密才生效
public class SysUser {

	/**
	 * 手机号码,加密
	 */
	@TableField(typeHandler = EncryptHandler.class)
	private String mobile;
}

方式二:返回的是自定义对象 (xml的方式)

@Data
public class AmountVO implements Serializable {

	/**
	 * 收款人账号,加密
	 */
	private String payeeAccount;

	/**
	 * 收款人姓名,加密
	 */
	private String payeeRealName;

}

	
		
		
	

	

方式三:返回的是自定义对象 (注解的方式)

@Data
public class AmountVO implements Serializable {

	/**
	 * 收款人账号,加密
	 */
	private String payeeAccount;

	/**
	 * 收款人姓名,加密
	 */
	private String payeeRealName;

}
@Results({
    @Result(column = "payee_account", property = "payeeAccount", typeHandler = EncryptHandler.class),
    @Result(column = "payee_real_name", property = "payeeRealName", typeHandler = EncryptHandler.class)})
@Select("select * from Amount where userId = #{userId }")
List findPage(Long userId);

 查询

// 使用Encrypt.encrypt把数据加密进行搜索
Long mobileCount = new LambdaQueryChainWrapper<>(sysUserMapper).eq(SysUser::getMobile, Encrypt.encrypt(dto.getMobile())).count();

效果如下:

添加数据后数据库是加密数据

springboot数据库敏感数据加密解密_第5张图片

 查询数据时,数据显示的是解密后的数据(数据显示成***,请查看博文数据显示成星号*号 字段脱敏) ​​​​​​springboot数据库敏感数据加密解密_第6张图片

结语:

springboot数据库敏感数据加密解密_第7张图片

你可能感兴趣的:(java,mybatis,plus,java,敏感数据加密)