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

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

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

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

痛點一:

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

痛點二:

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

脫敏配置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ù)源之前,需要準備一個 EncryptRuleConfiguration進行脫敏的配置,以下是一個例子,對于同一個數(shù)據(jù)源里兩張表card_infopay_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;
}

說明:

  • 創(chuàng)建 EncryptColumnRuleConfiguration 的時候有四個參數(shù),前兩個參數(shù)分表叫plainColumn、cipherColumn,其意思是數(shù)據(jù)庫存儲里面真實的兩個列(名文列、脫敏列),對于新的系統(tǒng),只需要設置脫敏列即可,所以以上示例為plainColumn為””。
  • 創(chuàng)建EncryptTableRuleConfiguration 的時候需要傳入一個map,這個map存的value即#1中說明的EncryptColumnRuleConfiguration ,而其key則是一個邏輯列,對于新系統(tǒng),此邏輯列即為真實的脫敏列。Sharding Shpere在攔截到SQL改寫的時候,會按照用戶的配置,把邏輯列映射為名文列或者脫敏列(默認)如下的示例

3.使用Sharding Sphere的數(shù)據(jù)源進行管理

把原始的數(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
# 默認的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

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

相關文章

  • SpringBoot集成selenium實現(xiàn)自動化測試的代碼工程

    SpringBoot集成selenium實現(xiàn)自動化測試的代碼工程

    Selenium?是支持web?瀏覽器自動化的一系列工具和[庫]?它提供了擴展來模擬用戶與瀏覽器的交互,用于擴展瀏覽器分配的分發(fā),本文給大家介紹了SpringBoot集成selenium實現(xiàn)自動化測試的代碼工程,需要的朋友可以參考下
    2024-08-08
  • Java常用流程控制語句實現(xiàn)原理解析

    Java常用流程控制語句實現(xiàn)原理解析

    這篇文章主要介紹了Java常用流程控制語句實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Spring Security異步無法獲取用戶認證信息的解決方法

    Spring Security異步無法獲取用戶認證信息的解決方法

    最近使用 Springboot 中 @Async 注解異步調用方法時,發(fā)現(xiàn)無法獲取到用戶認證信息,本文小編給大家介紹了Spring Security異步無法獲取用戶認證信息的原因和解決方法,并通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2024-09-09
  • SpringBoot如何實現(xiàn)同域SSO(單點登錄)

    SpringBoot如何實現(xiàn)同域SSO(單點登錄)

    單點登錄(SingleSignOn,SSO),就是通過用戶的一次性鑒別登錄。即在多個應用系統(tǒng)中,只需要登錄一次,就可以訪問其他相互信任的應用系統(tǒng),本文將介紹SpringBoot如何實現(xiàn)同域SSO(單點登錄)
    2021-05-05
  • Intellij IDEA命令行執(zhí)行java無法加載主類解決方案

    Intellij IDEA命令行執(zhí)行java無法加載主類解決方案

    這篇文章主要介紹了Intellij IDEA命令行執(zhí)行java無法加載主類解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Spring Security permitAll()不允許匿名訪問的操作

    Spring Security permitAll()不允許匿名訪問的操作

    這篇文章主要介紹了Spring Security permitAll()不允許匿名訪問的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java聊天室之實現(xiàn)使用Socket傳遞音頻

    Java聊天室之實現(xiàn)使用Socket傳遞音頻

    這篇文章主要為大家詳細介紹了Java簡易聊天室之使用Socket實現(xiàn)傳遞音頻功能,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以了解一下
    2022-10-10
  • Nacos通過RefreshScope實現(xiàn)配置自動更新的方式分享

    Nacos通過RefreshScope實現(xiàn)配置自動更新的方式分享

    這篇文章主要給大家介紹了Nacos如何通過RefreshScope實現(xiàn)配置自動更新,文中給了兩種實現(xiàn)方式供大家參考,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-09-09
  • Java 在Excel單元格中應用一種/多種字體樣式(實例代碼)

    Java 在Excel單元格中應用一種/多種字體樣式(實例代碼)

    這篇文章主要介紹了Java 在Excel單元格中應用一種/多種字體樣式,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Maven工程引入依賴失敗Dependencies全部飄紅問題

    Maven工程引入依賴失敗Dependencies全部飄紅問題

    這篇文章主要介紹了Maven工程引入依賴失敗Dependencies全部飄紅問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論