欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用SpringBoot整合Sharding Sphere實(shí)現(xiàn)數(shù)據(jù)脫敏的示例

 更新時(shí)間:2025年06月10日 12:02:29   作者:程序員小假  
Apache ShardingSphere數(shù)據(jù)脫敏模塊,通過SQL攔截與改寫實(shí)現(xiàn)敏感信息加密存儲(chǔ),解決手動(dòng)處理繁瑣及系統(tǒng)改造難題,支持Spring和Spring Boot配置,本文通過實(shí)例代碼介紹如何使用SpringBoot整合Sharding Sphere實(shí)現(xiàn)數(shù)據(jù)脫敏,感興趣的朋友一起看看吧

在真實(shí)業(yè)務(wù)場景中,數(shù)據(jù)庫中經(jīng)常需要存儲(chǔ)某些客戶的關(guān)鍵性敏感信息如:身份證號、銀行卡號、姓名、手機(jī)號碼等,此類信息按照合規(guī)要求,通常需要實(shí)現(xiàn)加密存儲(chǔ)以滿足合規(guī)要求。 

痛點(diǎn)一:

通常的解決方案是書寫SQL的時(shí)候,把對應(yīng)的加密字段手動(dòng)進(jìn)行加密再進(jìn)行插入,在查詢的時(shí)候使用之前再手動(dòng)進(jìn)行解密。此方法固然可行,但是使用起來非常不便捷且繁瑣,使得日常的業(yè)務(wù)開發(fā)與存儲(chǔ)合規(guī)的細(xì)節(jié)緊耦合 

痛點(diǎn)二:

對于一些為了快速上線而一開始沒有實(shí)現(xiàn)合規(guī)脫敏的系統(tǒng),如何比較快速的使得已有業(yè)務(wù)滿足合規(guī)要求的同時(shí),盡量減少對原系統(tǒng)的改造。(通常的這個(gè)過程至少包括:1.新增脫敏列的存儲(chǔ) 2.同時(shí)做數(shù)據(jù)遷移 3.業(yè)務(wù)的代碼做兼容邏輯等)。
Apache ShardingSphere下面存在一個(gè)數(shù)據(jù)脫敏模塊,此模塊集成的常用的數(shù)據(jù)脫敏的功能。其基本原理是對用戶輸入的SQL進(jìn)行解析攔截,并依靠用戶的脫敏配置進(jìn)行SQL的改寫,從而實(shí)現(xiàn)對原文字段的加密及加密字段的解密。最終實(shí)現(xiàn)對用戶無感的加解密存儲(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ī)則對象

在創(chuàng)建數(shù)據(jù)源之前,需要準(zhǔn)備一個(gè) EncryptRuleConfiguration進(jìn)行脫敏的配置,以下是一個(gè)例子,對于同一個(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ù)庫存儲(chǔ)里面真實(shí)的兩個(gè)列(名文列、脫敏列),對于新的系統(tǒng),只需要設(shè)置脫敏列即可,所以以上示例為plainColumn為””。
  • 創(chuàng)建EncryptTableRuleConfiguration 的時(shí)候需要傳入一個(gè)map,這個(gè)map存的value即#1中說明的EncryptColumnRuleConfiguration ,而其key則是一個(gè)邏輯列,對于新系統(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 銀行卡號 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

到此這篇關(guān)于如何用SpringBoot整合Sharding Sphere實(shí)現(xiàn)數(shù)據(jù)脫敏的文章就介紹到這了,更多相關(guān)SpringBoot Sharding Sphere數(shù)據(jù)脫敏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論