起源:https://jaskey.github.io/blog...

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

痛点一:

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

痛点二:

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

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

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

以下介绍基于Spring如何疾速让零碎反对脱敏配置。

1.引入依赖

<!-- for spring namespace --><dependency>    <groupId>org.apache.shardingsphere</groupId>    <artifactId>sharding-jdbc-spring-namespace</artifactId>    <version>${sharding-sphere.version}</version></dependency>

2.创立脱敏配置规定对象

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

private EncryptRuleConfiguration getEncryptRuleConfiguration() {    Properties props = new Properties();    //自带aes算法须要    props.setProperty("aes.key.value", aeskey);    EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("AES", props);    //自定义算法    //props.setProperty("qb.finance.aes.key.value", aeskey);    //EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("QB-FINANCE-AES", props);    EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration();    encryptRuleConfig.getEncryptors().put("aes", encryptorConfig);    //START: card_info 表的脱敏配置    {        EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("", "name", "", "aes");        EncryptColumnRuleConfiguration columnConfig2 = new EncryptColumnRuleConfiguration("", "id_no", "", "aes");        EncryptColumnRuleConfiguration columnConfig3 = new EncryptColumnRuleConfiguration("", "finshell_card_no", "", "aes");        Map<String, EncryptColumnRuleConfiguration> columnConfigMaps = new HashMap<>();        columnConfigMaps.put("name", columnConfig1);        columnConfigMaps.put("id_no", columnConfig2);        columnConfigMaps.put("finshell_card_no", columnConfig3);        EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columnConfigMaps);        encryptRuleConfig.getTables().put("card_info", tableConfig);    }    //END: card_info 表的脱敏配置    //START: pay_order 表的脱敏配置    {        EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("", "card_no", "", "aes");        Map<String, EncryptColumnRuleConfiguration> columnConfigMaps = new HashMap<>();        columnConfigMaps.put("card_no", columnConfig1);        EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columnConfigMaps);        encryptRuleConfig.getTables().put("pay_order", tableConfig);    }    log.info("脱敏配置构建实现:{} ", encryptRuleConfig);    return encryptRuleConfig;}

阐明:

  1. 创立 EncryptColumnRuleConfiguration 的时候有四个参数,前两个参数分表叫plainColumn、cipherColumn,其意思是数据库存储外面实在的两个列(名文列、脱敏列),对于新的零碎,只须要设置脱敏列即可,所以以上示例为plainColumn为”“。
  2. 创立EncryptTableRuleConfiguration 的时候须要传入一个map,这个map存的value即#1中阐明的EncryptColumnRuleConfiguration ,而其key则是一个逻辑列,对于新零碎,此逻辑列即为实在的脱敏列。Sharding Shpere在拦挡到SQL改写的时候,会依照用户的配置,把逻辑列映射为名文列或者脱敏列(默认)如下的示例

3.应用Sharding Sphere的数据源进行治理

把原始的数据源包装一层

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

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

以下步骤应用Spring Boot治理,能够仅用配置文件解决:

1.引入依赖

<!-- for spring boot --><dependency><groupId>org.apache.shardingsphere</groupId>    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>    <version>${sharding-sphere.version}</version></dependency><!-- for spring namespace --><dependency>    <groupId>org.apache.shardingsphere</groupId>    <artifactId>sharding-jdbc-spring-namespace</artifactId>    <version>${sharding-sphere.version}</version></dependency>

2.Spring 配置文件

spring.shardingsphere.datasource.name=dsspring.shardingsphere.datasource.ds.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds.url=xxxxxxxxxxxxxspring.shardingsphere.datasource.ds.username=xxxxxxxspring.shardingsphere.datasource.ds.password=xxxxxxxxxxxx# 默认的AES加密器spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aesspring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=hkiqAXU6Ur5fixGHaO4Lb2V2ggausYwW# card_info 姓名 AES加密spring.shardingsphere.encrypt.tables.card_info.columns.name.cipherColumn=namespring.shardingsphere.encrypt.tables.card_info.columns.name.encryptor=encryptor_aes# card_info 身份证 AES加密spring.shardingsphere.encrypt.tables.card_info.columns.id_no.cipherColumn=id_nospring.shardingsphere.encrypt.tables.card_info.columns.id_no.encryptor=encryptor_aes# card_info 银行卡号 AES加密spring.shardingsphere.encrypt.tables.card_info.columns.finshell_card_no.cipherColumn=finshell_card_nospring.shardingsphere.encrypt.tables.card_info.columns.finshell_card_no.encryptor=encryptor_aes# pay_order 银行卡号 AES加密spring.shardingsphere.encrypt.tables.pay_order.columns.card_no.cipherColumn=card_nospring.shardingsphere.encrypt.tables.pay_order.columns.card_no.encryptor=encryptor_aes

近期热文举荐:

1.1,000+ 道 Java面试题及答案整顿(2021最新版)

2.终于靠开源我的项目弄到 IntelliJ IDEA 激活码了,真香!

3.阿里 Mock 工具正式开源,干掉市面上所有 Mock 工具!

4.Spring Cloud 2020.0.0 正式公布,全新颠覆性版本!

5.《Java开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞+转发哦!