基于 Sharding Sphere,实现数据 “一键脱敏”!

数据库 其他数据库
在真实业务场景中,数据库中经常需要存储某些客户的关键性敏感信息如:身份证号、银行卡号、姓名、手机号码等,此类信息按照合规要求,通常需要实现加密存储以满足合规要求。

 在真实业务场景中,数据库中经常需要存储某些客户的关键性敏感信息如:身份证号、银行卡号、姓名、手机号码等,此类信息按照合规要求,通常需要实现加密存储以满足合规要求。

痛点一:

通常的解决方案是我们书写SQL的时候,把对应的加密字段手动进行加密再进行插入,在查询的时候使用之前再手动进行解密。此方法固然可行,但是使用起来非常不便捷且繁琐,使得日常的业务开发与存储合规的细节紧耦合

痛点二:

对于一些为了快速上线而一开始没有实现合规脱敏的系统,如何比较快速的使得已有业务满足合规要求的同时,尽量减少对原系统的改造。(通常的这个过程至少包括:1.新增脱敏列的存储 2.同时做数据迁移 3.业务的代码做兼容逻辑等)。

Apache ShardingSphere下面存在一个数据脱敏模块,此模块集成的常用的数据脱敏的功能。其基本原理是对用户输入的SQL进行解析拦截,并依靠用户的脱敏配置进行SQL的改写,从而实现对原文字段的加密及加密字段的解密。最终实现对用户无感的加解密存储、查询。

脱敏配置Quick Start——Spring 显示配置:

以下介绍基于Spring如何快速让系统支持脱敏配置。Spring Boot 学习笔记,推荐给你学习下。

1.引入依赖 

  1. <!-- for spring namespace -->  
  2. <dependency>  
  3.     <groupId>org.apache.shardingsphere</groupId>  
  4.     <artifactId>sharding-jdbc-spring-namespace</artifactId>  
  5.     <version>${sharding-sphere.version}</version>  
  6. </dependency> 

2.创建脱敏配置规则对象

在创建数据源之前,需要准备一个EncryptRuleConfiguration进行脱敏的配置,以下是一个例子,对于同一个数据源里两张表card_info,pay_order的不同字段进行AES的加密: 

  1. private EncryptRuleConfiguration getEncryptRuleConfiguration() {  
  2.     Properties props = new Properties();  
  3.     //自带aes算法需要  
  4.     props.setProperty("aes.key.value", aeskey);  
  5.     EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("AES", props);  
  6.     //自定义算法  
  7.     //props.setProperty("qb.finance.aes.key.value", aeskey);  
  8.     //EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("QB-FINANCE-AES", props);  
  9.     EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration();  
  10.     encryptRuleConfig.getEncryptors().put("aes", encryptorConfig);  
  11.     //START: card_info 表的脱敏配置  
  12.     {  
  13.         EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("", "name", "", "aes");  
  14.         EncryptColumnRuleConfiguration columnConfig2 = new EncryptColumnRuleConfiguration("", "id_no", "", "aes");  
  15.         EncryptColumnRuleConfiguration columnConfig3 = new EncryptColumnRuleConfiguration("", "finshell_card_no", "", "aes");  
  16.         Map<String, EncryptColumnRuleConfiguration> columnConfigMaps = new HashMap<>();  
  17.         columnConfigMaps.put("name", columnConfig1);  
  18.         columnConfigMaps.put("id_no", columnConfig2);  
  19.         columnConfigMaps.put("finshell_card_no", columnConfig3);  
  20.         EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columnConfigMaps);  
  21.         encryptRuleConfig.getTables().put("card_info", tableConfig);  
  22.     }  
  23.     //END: card_info 表的脱敏配置  
  24.     //START: pay_order 表的脱敏配置  
  25.     { 
  26.          EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("", "card_no", "", "aes");  
  27.         Map<String, EncryptColumnRuleConfiguration> columnConfigMaps = new HashMap<>();  
  28.         columnConfigMaps.put("card_no", columnConfig1);  
  29.         EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columnConfigMaps);  
  30.         encryptRuleConfig.getTables().put("pay_order", tableConfig);  
  31.     }  
  32.     log.info("脱敏配置构建完成:{} ", encryptRuleConfig);  
  33.     return encryptRuleConfig;  

说明:

    1. 创建 EncryptColumnRuleConfiguration 的时候有四个参数,前两个参数分表叫plainColumn、cipherColumn,其意思是数据库存储里面真实的两个列(名文列、脱敏列),对于新的系统,只需要设置脱敏列即可,所以以上示例为plainColumn为”“。

    2. 创建EncryptTableRuleConfiguration 的时候需要传入一个map,这个map存的value即#1中说明的EncryptColumnRuleConfiguration ,而其key则是一个逻辑列,对于新系统,此逻辑列即为真实的脱敏列。Sharding Shpere在拦截到SQL改写的时候,会按照用户的配置,把逻辑列映射为名文列或者脱敏列(默认)如下的示例

3.使用Sharding Sphere的数据源进行管理

把原始的数据源包装一层 

  1. @Bean("tradePlatformDataSource")   
  2. public DataSource dataSource(  
  3.  @Qualifier("druidDataSource") DataSource ds) throws SQLException {  
  4.  return EncryptDataSourceFactory.createDataSource(ds, getEncryptRuleConfiguration(), new Properties());   

脱敏配置Quick Start——Spring Boot版:

以下步骤使用Spring Boot管理,可以仅用配置文件解决:

1.引入依赖 

  1. <!-- for spring boot -->  
  2. <dependency>  
  3. <groupId>org.apache.shardingsphere</groupId>  
  4.     <artifactId>sharding-jdbc-spring-boot-starter</artifactId>  
  5.     <version>${sharding-sphere.version}</version>  
  6. </dependency>  
  7. <!-- for spring namespace -->  
  8. <dependency>  
  9.     <groupId>org.apache.shardingsphere</groupId>  
  10.     <artifactId>sharding-jdbc-spring-namespace</artifactId>  
  11.     <version>${sharding-sphere.version}</version>  
  12. </dependency> 

2.Spring 配置文件 

  1. spring.shardingsphere.datasource.name=ds  
  2. spring.shardingsphere.datasource.ds.type=com.alibaba.druid.pool.DruidDataSource  
  3. spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driver  
  4. spring.shardingsphere.datasource.ds.url=xxxxxxxxxxxxx 
  5. spring.shardingsphere.datasource.ds.username=xxxxxxx  
  6. spring.shardingsphere.datasource.ds.password=xxxxxxxxxxxx  
  7. # 默认的AES加密器  
  8. spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes  
  9. spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=hkiqAXU6Ur5fixGHaO4Lb2V2ggausYwW  
  10. # card_info 姓名 AES加密  
  11. spring.shardingsphere.encrypt.tables.card_info.columns.name.cipherColumn=name  
  12. spring.shardingsphere.encrypt.tables.card_info.columns.name.encryptor=encryptor_aes  
  13. # card_info 身份证 AES加密  
  14. spring.shardingsphere.encrypt.tables.card_info.columns.id_no.cipherColumn=id_no  
  15. spring.shardingsphere.encrypt.tables.card_info.columns.id_no.encryptor=encryptor_aes  
  16. # card_info 银行卡号 AES加密  
  17. spring.shardingsphere.encrypt.tables.card_info.columns.finshell_card_no.cipherColumn=finshell_card_no  
  18. spring.shardingsphere.encrypt.tables.card_info.columns.finshell_card_no.encryptor=encryptor_aes  
  19. # pay_order 银行卡号 AES加密  
  20. spring.shardingsphere.encrypt.tables.pay_order.columns.card_no.cipherColumn=card_no  
  21. spring.shardingsphere.encrypt.tables.pay_order.columns.card_no.encryptor=encryptor_aes 

另外,关注公众号Java技术栈,在后台回复:面试,可以获取我整理的 Java 系列面试题和答案,非常齐全。 

 

责任编辑:庞桂玉 来源: Java技术栈
相关推荐

2022-05-16 08:50:23

数据脱加密器

2020-11-24 11:00:24

前端

2012-03-01 14:00:08

2015-02-09 15:25:52

换肤

2021-02-22 11:07:49

Windows 10微软数据传输

2011-09-15 19:05:49

windows 7一键关机

2022-07-22 14:32:29

账号登录服务鸿蒙

2015-11-03 15:29:49

ONOS开放网络操作系统SDN

2022-07-04 11:28:14

RancherK8s集群云计算

2010-11-08 13:58:14

Check Point互联网安全移动访问软件刀片

2015-07-30 16:18:14

企业网D1Net

2022-07-27 14:59:20

原子化服务鸿蒙

2022-06-21 14:44:38

接口数据脱敏

2021-10-08 08:58:35

MySQL函数脱敏

2023-06-15 10:00:00

Jenkins任务操作

2020-05-14 12:09:56

Centos7EFK部署

2022-04-07 13:56:13

前端一键换肤

2021-04-23 10:38:52

Spring BootSpringMVC源码

2021-12-02 07:50:29

分支服务git worktre

2018-03-20 12:30:44

iOSPush抓包
点赞
收藏

51CTO技术栈公众号