数据脱敏实战
实战代码示例: https://gitee.com/ixinglan/mysql-demo.git
sharding-jdbc-demo: 详见README.md 介绍
我们以c_user表示例,只需1个库即可
CREATE TABLE `c_user` (
`Id` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT NULL,
`pwd_plain` varchar(256) DEFAULT NULL,
`pwd_cipher` varchar(256) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- entity创建: CUser, 注意: pwd为逻辑列,对应数据库中明文和密文列
- repository创建: CUserRepository
- 创建application-encrypt配置文件, 将主配置文件active改为encrypt
# datasource
spring:
shardingsphere:
datasource:
names: ds0
ds0:
type: com.zaxxer.hikari.HikariDataSource
driver-class: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/sharding-demo1
username: root
password: 123456
# 指定是否用密文列作查询
props:
query:
with:
cipher:
column: true
#encrypt
encrypt:
tables:
c_user:
columns:
pwd:
plainColumn: pwd_plain # 逻辑列pwd对应的明文列
cipherColumn: pwd_cipher #逻辑列pwd对应的密文列
encryptor: demo_pwd #下面定义的自定义名称
# 主键生成器
keyGenerator:
column: id
type: SNOWFLAKE
#指定算法
encryptors:
demo_pwd: #自定义名称
type: aes
props:
aes:
key:
value: 1234 #密钥
- 测试方法 TestEncryptor: testAdd, testFind