一、介绍

在实际的软件系统开发过程中,由于业务的需求,在代码层面实现数据的脱敏还是远远不够的,往往还需要在数据库层面针对某些关键性的敏感信息,例如:身份证号、银行卡号、手机号、工资等信息进行加密存储,实现真正意义的数据混淆脱敏,以满足信息安全的需要。

那在实际的业务开发过程中,我们如何快速实现呢?

今天通过这篇文章,我们一起来了解一下如何在 Spring Boot 中快速实现数据的加解密功能。废话不多说了,直接撸代码!

二、方案实践

在 Spring Boot 生态中,有一个非常厉害的开源框架:Apache ShardingSphere。

它是一款分布式 SQL 事务和查询引擎,可通过数据分片、弹性伸缩、加密等能力对任意数据库进行增强。我们可以利用它的数据脱敏模块,快速实现 SQL 字段的加解密操作。

如果当前项目是采用 Spring Boot 开发的,可以实现无缝集成,对原系统的改造会非常少。

下面以用户表为例,一起了解一下ShardingSphere的数据加解密的实现过程!

2.1、创建用户表

首先,在数据库中创建一张用户表,示例脚本如下!

CREATE TABLE user (
id bigint(20) NOT NULL COMMENT '用户ID',
email varchar(255) NOT NULL DEFAULT '' COMMENT '邮件',
nick_name varchar(255) DEFAULT NULL COMMENT '昵称',
pass_word varchar(255) NOT NULL DEFAULT '' COMMENT '二次密码',
reg_time varchar(255) NOT NULL DEFAULT '' COMMENT '注册时间',
user_name varchar(255) NOT NULL DEFAULT '' COMMENT '用户名',
salary varchar(255) DEFAULT NULL COMMENT '基本工资',
PRIMARY KEY (id) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

2.2、创建 springboot 项目并添加依赖包

接着,创建一个 Spring Boot 项目,并添加相关的依赖包,示例如下:

<dependencies>
<!--spring boot核心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--spring boot 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springmvc web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql 数据源-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis 支持-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--shardingsphere数据分片、脱敏工具-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>

2.3、添加相关配置

application.properties文件中,添加shardingsphere相关配置,即可实现针对某个表进行脱敏

server.port=8080

logging.path=log

#shardingsphere数据源集成
spring.shardingsphere.datasource.name=ds
spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://127.0.0.1:3306/test
spring.shardingsphere.datasource.ds.username=xxxx
spring.shardingsphere.datasource.ds.password=xxxx #加密方式、密钥配置
spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes
spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=hkiqAXU6Ur5fixGHaO4Lb2V2ggausYwW
#plainColumn表示明文列,cipherColumn表示脱敏列
spring.shardingsphere.encrypt.tables.user.columns.salary.plainColumn=
spring.shardingsphere.encrypt.tables.user.columns.salary.cipherColumn=salary
#spring.shardingsphere.encrypt.tables.user.columns.pass_word.assistedQueryColumn=
spring.shardingsphere.encrypt.tables.user.columns.salary.encryptor=encryptor_aes #sql打印
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.query.with.cipher.column=true #基于xml方法的配置
mybatis.mapper-locations=classpath:mapper/*.xml

其中有几个的配置信息比较重要,spring.shardingsphere.encrypt.tables是指要脱敏的表,user是表名,salary表示user表中的真实列,其中plainColumn指的是明文列,cipherColumn指的是脱敏列,如果是新工程,只需要配置脱敏列即可!

配置示例如下!

# 用于告诉 ShardingSphere 数据表里哪个列用于存储明文数据
spring.shardingsphere.encrypt.tables.user.columns.salary.plainColumn=
# 用于告诉 ShardingSphere 数据表里哪个列用于存储密文数据
spring.shardingsphere.encrypt.tables.user.columns.salary.cipherColumn=salary
# 用于告诉 ShardingSphere 数据表里哪个列用于存储辅助查询数据
#spring.shardingsphere.encrypt.tables.user.columns.salary.assistedQueryColumn=
# 用于告诉 ShardingSphere 数据表里哪个列使用什么算法加解密
spring.shardingsphere.encrypt.tables.user.columns.salary.encryptor=encryptor_aes

2.4、编写数据持久层

然后,编写一个数据持久层,用于数据的存储和查询操作。

<mapper namespace="com.example.shardingsphere.mapper.UserMapperXml" >

    <resultMap id="BaseResultMap" type="com.example.shardingsphere.entity.UserEntity" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
<result column="pass_word" property="passWord" jdbcType="VARCHAR" />
<result column="reg_time" property="regTime" jdbcType="VARCHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="salary" property="salary" jdbcType="VARCHAR" />
</resultMap> <select id="findAll" resultMap="BaseResultMap">
SELECT * FROM user
</select> <insert id="insert" parameterType="com.example.shardingsphere.entity.UserEntity">
INSERT INTO user(id,email,nick_name,pass_word,reg_time,user_name, salary)
VALUES(#{id},#{email},#{nickName},#{passWord},#{regTime},#{userName}, #{salary})
</insert>
</mapper>
public interface UserMapperXml {

    /**
* 查询所有的信息
* @return
*/
List<UserEntity> findAll(); /**
* 新增数据
* @param user
*/
void insert(UserEntity user);
}
public class UserEntity {

    private Long id;

    private String email;

    private String nickName;

    private String passWord;

    private String regTime;

    private String userName;

    private String salary;

    //省略set、get...

}

2.5、单元测试

最后,我们编写一个单元测试,验证一下代码的正确性。

编写启用服务程序

@SpringBootApplication
@MapperScan("com.example.shardingsphere.mapper")
public class ShardingSphereApplication { public static void main(String[] args) {
SpringApplication.run(ShardingSphereApplication.class, args);
}
}

编写单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ShardingSphereApplication.class)
public class UserTest { @Autowired
private UserMapperXml userMapperXml; @Test
public void insert() throws Exception {
UserEntity entity = new UserEntity();
entity.setId(3l);
entity.setEmail("123@123.com");
entity.setNickName("阿三");
entity.setPassWord("123");
entity.setRegTime("2021-10-10 00:00:00");
entity.setUserName("张三");
entity.setSalary("2500");
userMapperXml.insert(entity);
} @Test
public void query() throws Exception {
List<UserEntity> dataList = userMapperXml.findAll();
System.out.println(JSON.toJSONString(dataList));
}
}

插入数据后,如下图,数据库存储的数据已被加密!

我们继续来看看,运行查询服务,结果如下图,数据被成功解密!

采用配置方式,最大的好处就是直接通过配置脱敏列就可以完成对某些数据表字段的脱敏,非常方便。

三、小结

当需要对某些数据表字段进行脱敏处理的时候,可以采用 Apache ShardingSphere 框架快速实现。

但是有个细节很容易遗漏,那就是字段类型,例如salary字段,根据常规,很容易想到使用数字类型,但是却不是,要知道加密之后的数据都是一串乱码,数字类型肯定是无法存储字符串的,因此在定义的时候,这个要留心一下。

希望以上的案例,能帮助到大家!

示例代码:spring-boot-example-shardingsphere

SpringBoot+ Sharding Sphere 轻松实现数据库字段加解密的更多相关文章

  1. SpringBoot+ShardingSphere彻底解决生产环境数据库字段加解密问题

    前言   互联网行业公司,对于数据库的敏感字段是一定要进行加密的,方案有很多,最直接的比如写个加解密的工具类,然后在每个业务逻辑中手动处理,在稍微有点规模的项目中这种方式显然是不现实的,不仅工作量大而 ...

  2. 惊呆了!不改一行 Java 代码竟然就能轻松解决敏感信息加解密|原创

    前言 出于安全考虑,现需要将数据库的中敏感信息加密存储到数据库中,但是正常业务交互还是需要使用明文数据,所以查询返回我们还需要经过相应的解密才能返回给调用方. ps:日常开发中,我们要有一定的安全意识 ...

  3. java实现工程配置文件敏感字段加解密

    以下引自他人博客: 1. 需求背景我们在开发应用时,需要连接数据库,一般把数据库信息放在一个属性配置文件中,比如***.properties,具体的内容 #mysql的配置文件jdbc.url=jdb ...

  4. 从零开始实现一个MyBatis加解密插件

    作者:vivo 互联网服务器团队- Li Gang 本篇文章介绍使用MyBatis插件来实现数据库字段加解密的过程. 一.需求背景 公司出于安全合规的考虑,需要对明文存储在数据库中的部分字段进行加密, ...

  5. .NET Core加解密实战系列之——消息摘要与数字签名算法

    目录 简介 功能依赖 消息摘要算法 MD算法 家族发展史 应用场景 代码实现 MD5 示例代码 SHA算法 应用场景 代码实现 SHA1 SHA256 示例代码 MAC算法 HMAC算法的典型应用 H ...

  6. .NET Core加解密实战系列之——使用BouncyCastle制作p12(.pfx)数字证书

    简介 加解密现状,编写此系列文章的背景: 需要考虑系统环境兼容性问题(Linux.Windows) 语言互通问题(如C#.Java等)(加解密本质上没有语言之分,所以原则上不存在互通性问题) 网上资料 ...

  7. Android数据库安全解决方案,使用SQLCipher进行加解密

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952409 我们都知道,Android系统内置了SQLite数据库,并且提供了一 ...

  8. .NET Core加解密实战系列之——对称加密算法

    简介 加解密现状,编写此系列文章的背景: 需要考虑系统环境兼容性问题(Linux.Windows) 语言互通问题(如C#.Java等)(加解密本质上没有语言之分,所以原则上不存在互通性问题) 网上资料 ...

  9. 提取数据库字段里面的值,并改变+图片懒加载,jquery延迟加载

    要求:手机端打开某个页面的详细信息,因为网速或者别的原因,响应太慢,因为图片大的原因,希望先进来,图片在网页运行的情况再慢慢加载(jquer延迟加载) http://www.w3cways.com/1 ...

  10. 关于Oracle.ManagedDataAccess数据库表加字段后,必须重启的问题

    关于Oracle.ManagedDataAccess数据库表加字段后,必须重启的问题,解决方法如下:在数据库连接字串中,增加一个参数:Metadata Pooling=false如“Data Sour ...

随机推荐

  1. RocketMQ事务消息源码解析

    RocketMQ提供了事务消息的功能,采用2PC(两阶段协议)+补偿机制(事务回查)的分布式事务功能,通过这种方式能达到分布式事务的最终一致. 一. 概述 半事务消息:指的是发送至broker但是还没 ...

  2. conda错误 创建新环境conda create -n TF117 python=3.5时报错 An unexpected error has occurred. Conda has prepared the above report.

    创建新环境conda create -n TF117 python=3.5时报错 An unexpected error has occurred. Conda has prepared the ab ...

  3. Apisix网关-使用Grafana可视化Apisix的Prometheus数据

    Apisix安装部署 Apisix官网安装教程 Apisix Dashboard官网安装教程 本次教程使用RPM包安装方式 使用systemctl管理服务 Apisix配置要点 /usr/local/ ...

  4. 绘图与可视化--pandas中的绘图函数

    matplotlib API函数都位于matplotlib.pyplot模块中. 本节代码中引入的约定为:import matplotlib.pyplot as plt numpy库也会用到,约定:i ...

  5. 第二次大作业BLOG心得

    (1)前言: 知识点: ①ArrayLsit的知识点: ArrayList 是 Java 中的动态数组实现,它提供了自动调整大小的功能,可以根据需要动态增长或收缩. ArrayList 可以存储任意类 ...

  6. IDEA顺序启动多个Spring Boot微服务

    上个月公司新开发的一个项目,需要使用微服务,将单体服务拆分成多个微服务.但是每次修改代码之后都需要启动多个微服务,改个代码,都要修改五分钟,启动半小时,但是idea可以设置将多个服务依次启动,减少操作 ...

  7. js数据类型的检查

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  8. LeetCode 409. Longest Palindrome 最长回文串(C++/Java)

    题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest ...

  9. PB通过OLE方式调用C#.NET DLL时,DLL获取自身根目录

    PB通过OLE方式调用C#.NET DLL时, DLL获取自身根目录 System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExe ...

  10. 开源高性能结构化日志模块NanoLog

      最近在写数据库程序,需要一个高性能的结构化日志记录组件,简单研究了一下Microsoft.Extensions.Logging和Serilog,还是决定重造一个轮子. 一.使用方法   直接参考以 ...