使用SpringBoot整合Sharding Sphere實(shí)現(xiàn)數(shù)據(jù)脫敏的示例
在真實(shí)業(yè)務(wù)場(chǎng)景中,數(shù)據(jù)庫(kù)中經(jīng)常需要存儲(chǔ)某些客戶的關(guān)鍵性敏感信息如:身份證號(hào)、銀行卡號(hào)、姓名、手機(jī)號(hào)碼等,此類信息按照合規(guī)要求,通常需要實(shí)現(xiàn)加密存儲(chǔ)以滿足合規(guī)要求。
痛點(diǎn)一:
通常的解決方案是書寫SQL的時(shí)候,把對(duì)應(yīng)的加密字段手動(dòng)進(jìn)行加密再進(jìn)行插入,在查詢的時(shí)候使用之前再手動(dòng)進(jìn)行解密。此方法固然可行,但是使用起來非常不便捷且繁瑣,使得日常的業(yè)務(wù)開發(fā)與存儲(chǔ)合規(guī)的細(xì)節(jié)緊耦合
痛點(diǎn)二:
對(duì)于一些為了快速上線而一開始沒有實(shí)現(xiàn)合規(guī)脫敏的系統(tǒng),如何比較快速的使得已有業(yè)務(wù)滿足合規(guī)要求的同時(shí),盡量減少對(duì)原系統(tǒng)的改造。(通常的這個(gè)過程至少包括:1.新增脫敏列的存儲(chǔ) 2.同時(shí)做數(shù)據(jù)遷移 3.業(yè)務(wù)的代碼做兼容邏輯等)。
Apache ShardingSphere下面存在一個(gè)數(shù)據(jù)脫敏模塊,此模塊集成的常用的數(shù)據(jù)脫敏的功能。其基本原理是對(duì)用戶輸入的SQL進(jìn)行解析攔截,并依靠用戶的脫敏配置進(jìn)行SQL的改寫,從而實(shí)現(xiàn)對(duì)原文字段的加密及加密字段的解密。最終實(shí)現(xiàn)對(duì)用戶無感的加解密存儲(chǔ)、查詢。
脫敏配置Quick Start——Spring 顯示配置:
以下介紹基于Spring如何快速讓系統(tǒng)支持脫敏配置。
1.引入依賴
<!-- for spring namespace -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>2.創(chuàng)建脫敏配置規(guī)則對(duì)象
在創(chuàng)建數(shù)據(jù)源之前,需要準(zhǔn)備一個(gè) EncryptRuleConfiguration進(jìn)行脫敏的配置,以下是一個(gè)例子,對(duì)于同一個(gè)數(shù)據(jù)源里兩張表card_info,pay_order的不同字段進(jìn)行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("脫敏配置構(gòu)建完成:{} ", encryptRuleConfig);
return encryptRuleConfig;
}說明:
- 創(chuàng)建
EncryptColumnRuleConfiguration的時(shí)候有四個(gè)參數(shù),前兩個(gè)參數(shù)分表叫plainColumn、cipherColumn,其意思是數(shù)據(jù)庫(kù)存儲(chǔ)里面真實(shí)的兩個(gè)列(名文列、脫敏列),對(duì)于新的系統(tǒng),只需要設(shè)置脫敏列即可,所以以上示例為plainColumn為””。 - 創(chuàng)建
EncryptTableRuleConfiguration的時(shí)候需要傳入一個(gè)map,這個(gè)map存的value即#1中說明的EncryptColumnRuleConfiguration,而其key則是一個(gè)邏輯列,對(duì)于新系統(tǒng),此邏輯列即為真實(shí)的脫敏列。Sharding Shpere在攔截到SQL改寫的時(shí)候,會(huì)按照用戶的配置,把邏輯列映射為名文列或者脫敏列(默認(rèn))如下的示例

3.使用Sharding Sphere的數(shù)據(jù)源進(jìn)行管理
把原始的數(shù)據(jù)源包裝一層
@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 # 默認(rèn)的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 銀行卡號(hào) 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 銀行卡號(hào) 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
到此這篇關(guān)于如何用SpringBoot整合Sharding Sphere實(shí)現(xiàn)數(shù)據(jù)脫敏的文章就介紹到這了,更多相關(guān)SpringBoot Sharding Sphere數(shù)據(jù)脫敏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)用小技能之快速創(chuàng)建List常用幾種方式
java集合可以說無論是面試、刷題還是工作中都是非常常用的,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)用小技能之快速創(chuàng)建List常用的幾種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Java實(shí)現(xiàn)word/pdf轉(zhuǎn)html并在線預(yù)覽
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)word、pdf文件轉(zhuǎn)html并在線預(yù)覽的功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-05-05
elasticsearch通過guice注入Node組裝啟動(dòng)過程
這篇文章主要為大家介紹了?elasticsearch通過guice注入Node組裝啟動(dòng)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Java Kafka消費(fèi)者實(shí)現(xiàn)過程
Kafka消費(fèi)者通過KafkaConsumer類實(shí)現(xiàn),核心機(jī)制包括偏移量管理、消費(fèi)者組協(xié)調(diào)、批量拉取消息及多線程處理,手動(dòng)提交offset確保數(shù)據(jù)可靠性,自動(dòng)提交則依賴框架,本文給大家介紹Java Kafka消費(fèi)者實(shí)現(xiàn)過程,感興趣的朋友一起看看吧2025-08-08

