Spring Boot整合Sharding-JDBC实现数据脱敏

目录

背景

对互联网公司、传统行业来说,数据安全一直是极为重视和敏感的话题。数据脱敏是指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护。

Apache ShardingSphere根据业界对脱敏的需求及业务改造痛点,提供了一套完整、安全、透明化、低改造成本的数据脱敏整合解决方案。

ShardingSphere脱敏规则

脱敏配置主要分为四部分:数据源配置,加密器配置,脱敏表配置以及查询属性配置,其详情如下图所示:

数据源配置:是指DataSource的配置。

加密器配置:是指使用什么加密策略进行加解密。目前ShardingSphere内置了两种加解密策略:AES/MD5。用户还可以通过实现ShardingSphere提供的接口,自行实现一套加解密算法。

脱敏表配置:用于告诉ShardingSphere数据表里哪个列用于存储密文数据(cipherColumn)、哪个列用于存储明文数据(plainColumn)以及用户想使用哪个列进行SQL编写(logicColumn)。

查询属性的配置:当底层数据库表里同时存储了明文数据、密文数据后,该属性开关用于决定是直接查询数据库表里的明文数据进行返回,还是查询密文数据通过Encrypt-JDBC解密后返回。

脱敏处理流程图

sharding-jdbc数据脱敏

sharding-jdbc与spring boot集成之后,通过配置我们就可以使用sharding-jdbc的功能

数据脱敏配置

下面是单独配置数据脱敏的配置

java 复制代码
#数据源名称,多数据源以逗号分隔
spring.shardingsphere.datasource.name=ds

#数据源连接信息和相关配置
spring.shardingsphere.datasource.ds.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds.url=jdbc:mysql://127.0.0.1:3306/encrypt?serverTimezone=UTC&useSSL=false
spring.shardingsphere.datasource.ds.username=root
spring.shardingsphere.datasource.ds.password=
spring.shardingsphere.datasource.ds.max-total=100

spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes
spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=123456
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.plainColumn=user_decrypt
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.cipherColumn=user_encrypt
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.assistedQueryColumn=user_assisted
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.encryptor=encryptor_aes

#打印sql语句
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.query.with.cipher.column=true

数据源相关配置省略,可以看我之前的文章,下面主要介绍数据脱敏的相关配置

java 复制代码
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.encryptor=encryptor_aes

加密器名字,上述配置用于指定对"t_order"表的"user_id"列进行加密时使用的加密器,表和列是根据我们实际情况配置的

格式:spring.shardingsphere.encrypt.tables.<table-name>.columns.<logic-column-name>.encryptor

java 复制代码
spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes

配置加密器类型,可自定义或选择内置加密算法:MD5/AES

encryptor_aes为加密器的名称,对应我们定义的encryptor

java 复制代码
spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=123456

配置 AES 加密算法的密钥,AES算法必配

java 复制代码
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.plainColumn=user_decrypt

存储明文的字段,不需要存储可不配置

java 复制代码
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.cipherColumn=user_encrypt

存储密文的字段

java 复制代码
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.assistedQueryColumn=user_assisted

辅助查询字段,针对ShardingQueryAssistedEncryptor类型的加解密器进行辅助查询,一般不需要配置

java 复制代码
spring.shardingsphere.props.query.with.cipher.column=true

设置是否在查询结果中包含加密列的明文值,当将该属性设置为 true 时,ShardingSphere 在执行查询操作时,会将加密列的明文值包含在查询结果中返回

数据分片 + 数据脱敏配置

java 复制代码
spring.shardingsphere.datasource.names=ds_0,ds_1

spring.shardingsphere.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0
spring.shardingsphere.datasource.ds_0.username=root
spring.shardingsphere.datasource.ds_0.password=

spring.shardingsphere.datasource.ds_1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1
spring.shardingsphere.datasource.ds_1.username=root
spring.shardingsphere.datasource.ds_1.password=

spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds_$->{user_id % 2}

spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds_$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds_$->{0..1}.t_order_item_$->{0..1}
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order_item.key-generator.column=order_item_id
spring.shardingsphere.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.encrypt-rule.encryptors.encryptor_aes.type=aes
spring.shardingsphere.sharding.encrypt-rule.encryptors.encryptor_aes.props.aes.key.value=123456
spring.shardingsphere.sharding.encrypt-rule.tables.t_order.columns.user_id.plainColumn=user_decrypt
spring.shardingsphere.sharding.encrypt-rule.tables.t_order.columns.user_id.cipherColumn=user_encrypt
spring.shardingsphere.sharding.encrypt-rule.tables.t_order.columns.user_id.assistedQueryColumn=user_assisted
spring.shardingsphere.sharding.encrypt-rule.tables.t_order.columns.user_id.encryptor=encryptor_aes
相关推荐
倒流时光三十年20 小时前
PostgreSQL CASE 条件表达式详解
数据库·postgresql
挖坑的张师傅20 小时前
方便 Mac 本机运行 e2b 的沙箱方案 e2b-local
人工智能·后端
开心猴爷21 小时前
Flutter 如何自动上传 可以 IPA 把构建和上传分开处理
后端·ios
二月龙21 小时前
defer 执行顺序与底层原理,90% 的人都理解不全
后端
长大198821 小时前
新手常犯的 Go 语法错误,一次性帮你避坑
后端
小强198821 小时前
深入理解 Go 协程 Goroutine:并发编程的核心精髓
后端
chengliu050821 小时前
后端学习地图
后端
phltxy21 小时前
Spring AI 可观测性与 Zipkin 实战
java·人工智能·spring
字节跳动数据平台21 小时前
营销视频进入工业化时代,火山引擎多模态数据湖如何助力多米实现内容生产提效 100+ 倍
数据库
兰令水21 小时前
leecodecode【面试150】【2026.6.13打卡-java版本】
java·算法·leetcode