以下文章来源方志朋的博客,回复”666“获面试宝典

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

痛点一:

通常的解决方案是我们书写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=ds
spring.shardingsphere.datasource.ds.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds.url=xxxxxxxxxxxxx
spring.shardingsphere.datasource.ds.username=xxxxxxx
spring.shardingsphere.datasource.ds.password=xxxxxxxxxxxx# 默认的AES加密器
spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes
spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=hkiqAXU6Ur5fixGHaO4Lb2V2ggausYwW# card_info 姓名 AES加密
spring.shardingsphere.encrypt.tables.card_info.columns.name.cipherColumn=name
spring.shardingsphere.encrypt.tables.card_info.columns.name.encryptor=encryptor_aes# card_info 身份证 AES加密
spring.shardingsphere.encrypt.tables.card_info.columns.id_no.cipherColumn=id_no
spring.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_no
spring.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_no
spring.shardingsphere.encrypt.tables.pay_order.columns.card_no.encryptor=encryptor_aes

来源 | https://jaskey.github.io/blog/2020/03/18/sharding-sphere-data-desensitization/

热门内容:Spring发布新成员:Spring GraphQL!高调出场的GraphQL能火起来了吗?
很哇塞的Java系列实战项目!
翻车!在项目中用了Arrays.asList、ArrayList的subList,被公开批评60岁还在写代码的开发者,他的建议或许正是你现在焦虑的根源!尝试改变一下吧!
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

实现数据“一键脱敏”,Sharding Sphere帮你搞定相关推荐

  1. xis表格怎么打印_Excel表格过大,如何将数据打印在一张A4纸上?3种方法帮你搞定...

    原标题:Excel表格过大,如何将数据打印在一张A4纸上?3种方法帮你搞定 我们在工作时候,经常会打印Excel表,不知道你在打印Excel时候有没有遇到这样的情况,因为Excel表格过大,无法在一张 ...

  2. [喵咪的Liunx(1)]计划任务队列脚本后台进程Supervisor帮你搞定

    喵咪的Liunx(1)]计划任务队列脚本后台进程Supervisor帮你搞定 前言 哈喽大家好啊,好久不见啊(都快一个月了),要问为什么没有更新博客呢只应为最近在录制PhalApi的视频教程时间比较少 ...

  3. a类不确定度计算器_统统帮您搞定:LIMS系统,换版、内审、期间核查、不确定度、数据分析…………...

    CNAS实验室认可ISO17025三年的过渡期,所有获认可实验室应在2020年11月30日前完成新版CL01的转换工作.实验室转换工作的完成以取得依据ISO/IEC17025:2017颁发的认可证书为 ...

  4. python降低图片分辨率_手把手:扫描图片又大又不清晰?这个Python小程序帮你搞定!...

    原标题:手把手:扫描图片又大又不清晰?这个Python小程序帮你搞定! 大数据文摘作品 编译:HAPPEN.于乐源.小鱼 一位乐于分享学生精彩笔记的大学教授对于扫描版的文件非常不满意--颜色不清晰并且 ...

  5. 【干货知识】Redis:从应用到底层,一文帮你搞定

    1.基本类型及底层实现 1.1.String 用途: 适用于简单key-value存储.setnx key value实现分布式锁.计数器(原子性).分布式全局唯一ID. 底层:C语言中String用 ...

  6. 计算机上面的东西爆满怎么清理,电脑C盘满了怎么清理?一招帮你搞定

    在日常电脑的使用中,我们总是会发现C盘的东西越来越多,然而对于C盘里面的东西我们又不知道如何清理,很多系统文件又不懂识别,无用和缓存文件堆积的特别多,最后导致C盘爆满,电脑卡顿.运作效率慢.那么,当C ...

  7. 一篇文章帮你搞定JVM中的堆

    文章目录 一篇文章帮你搞定JVM中的堆 堆的核心概述 堆的内存细分 设置堆内存大小与OOM OOM(OutOfMemory)举例 年轻代与老年代 图解对象分配过程 MinorGC,MajorGC,Fu ...

  8. python查看微信撤回消息_想查看微信好友撤回的消息?Python帮你搞定

    要说微信最让人恶心的发明,消息撤回绝对能上榜. 比如你现在正和女朋友用微信聊着天,或者跟自己喜欢的女孩子聊着天,一个不留神,你没注意到对方发的消息就被她及时撤回了,这时你很好奇,好奇她到底发了什么?于 ...

  9. flac格式如何转mp3,3招帮你搞定

    flac格式如何转mp3,3招帮你搞定的方法来啦.当你的音频是flac格式是不是很头疼,又不知道怎么转mp3 .然后网上搜索出很多方法又不知道从哪个下手,是不是很疑惑?那今天就来看看小编推荐的方法吧, ...

最新文章

  1. 国家智慧城市战略实施 保温材料等建材万亿市场待挖掘
  2. 缓存与库先写哪个,这十几张图告诉你
  3. Python错误和异常学习
  4. 图模型(graphical model, GM)的表示
  5. SAP Analytics Cloud连接Cloud for Customer的一些后台调试
  6. [CodeForces 1603C] Extreme Extension(贪心 + 数论分块优化dp)
  7. [js] innerHTML有什么缺点?
  8. 【mongodb系统学习之三】进入mongodb shell
  9. react 16 对外暴露function_【第 25 期】React 架构的演变 从同步到异步(一)
  10. SSH2中 关于修改hbm.xml文件 中内容无效果的解决方式
  11. PHP PDO学习(二) exec执行SQL
  12. linux (debian) 配置静态ip
  13. js 关闭子页面刷新父页面
  14. ImageJ开发插件注意事项
  15. SSH移植到arm开发板
  16. iOS 13越狱:越狱后如何安装AppSync和afc2越狱补丁
  17. 《繁荣的真相》读书笔记
  18. 以喷管雷诺数,当地大气压及射流有效温度计算所需质量流量,静温
  19. p图软件pⅰc_‎修图神器 - 修改照片,美化图片p图工具 trên App Store
  20. html怎么修改td 的宽度,html td怎么设置宽度

热门文章

  1. 复旦 计算机 学硕 延毕,研究生招考呈现新趋势:非全日制招生遇冷 延期毕业现象越发明显...
  2. linux 基础命令一
  3. activiti5/6 系列之--BpmnModel使用
  4. pwn with glibc heap(堆利用手册)
  5. 【Linux笔记(002) 】-- centos7 文档操作基本命令
  6. 将Eclipse代码导入到AndroidStudio的两种方式
  7. JDE函数--GetUDC(B函数)
  8. 企业信息化中常见决策点应对
  9. 【直播】陈安东,但扬:CNN模型搭建、训练以及LSTM模型思路详解
  10. 技术图文:如何利用BigOne的API制作自动化交易系统 -- 订单系统