SpringBoot+ Sharding Sphere 轻松实现数据库字段加解密

一、介绍

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

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

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

二、方案实践

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

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

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

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

2.1、创建用户表

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

sql 复制代码
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 项目,并添加相关的依赖包,示例如下:

xml 复制代码
<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、编写数据持久层

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

xml 复制代码
<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>
java 复制代码
public interface UserMapperXml {


    /**
     * 查询所有的信息
     * @return
     */
    List<UserEntity> findAll();

    /**
     * 新增数据
     * @param user
     */
    void insert(UserEntity user);
}
java 复制代码
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、单元测试

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

编写启用服务程序

java 复制代码
@SpringBootApplication
@MapperScan("com.example.shardingsphere.mapper")
public class ShardingSphereApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShardingSphereApplication.class, args);
    }
}

编写单元测试

java 复制代码
@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("[email protected]");
        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

相关推荐
有什么东东13 小时前
山东大学软件学院创新项目实训开发日志(20)之中医知识问答自动生成对话标题bug修改
java·vue·bug·springboot
我爱布朗熊17 小时前
4.RabbitMQ - 延迟消息
rabbitmq·springboot
mask哥1 天前
详解springcloudalibaba采用prometheus+grafana实现服务监控
java·nacos·springboot·grafana·prometheus·springcloud·微服务监控
幽络源小助理1 天前
SpringBoot民宿管理系统开发实现
java·spring boot·springboot·民宿系统
古渡蓝按2 天前
微信支付功能的设计实现与关键实践(UniApp+Java)全代码
springboot
Nick_zcy3 天前
协同推荐算法实现的智能商品推荐系统 - [基于springboot +vue]
算法·springboot·推荐算法
洛阳泰山3 天前
LangChain4j 搭配 Kotlin:以协程、流式交互赋能语言模型开发
java·ai·语言模型·kotlin·交互·springboot·langchain4j
飞天小牛肉5 天前
拼多多面经,暑期实习Java一面
java·spring·springboot·秋招·校招
橘猫云计算机设计5 天前
django基于爬虫的网络新闻分析系统的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
后端·爬虫·python·django·毕业设计·springboot
南客先生5 天前
海量聊天数据处理:基于Spring Boot与SharingJDBC的分库分表策略及ClickHouse冷热数据分离
mysql·clickhouse·springboot·分库分表·大数据处理·sharingjdbc