<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          基于 Sharding Sphere,實現(xiàn)數(shù)據(jù) “一鍵脫敏”!

          共 5291字,需瀏覽 11分鐘

           ·

          2021-08-05 03:19

          點擊關(guān)注公眾號,Java干貨及時送達

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

          在真實業(yè)務場景中,數(shù)據(jù)庫中經(jīng)常需要存儲某些客戶的關(guān)鍵性敏感信息如:身份證號、銀行卡號、姓名、手機號碼等,此類信息按照合規(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)支持脫敏配置。Spring Boot 學習筆記推薦給你學習下。

          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_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("脫敏配置構(gòu)建完成:{} ", encryptRuleConfig);
              return encryptRuleConfig;

          }

          說明:

          1. 創(chuàng)建 EncryptColumnRuleConfiguration 的時候有四個參數(shù),前兩個參數(shù)分表叫plainColumn、cipherColumn,其意思是數(shù)據(jù)庫存儲里面真實的兩個列(名文列、脫敏列),對于新的系統(tǒng),只需要設置脫敏列即可,所以以上示例為plainColumn為”“。
          2. 創(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

          另外,關(guān)注公眾號Java技術(shù)棧,在后臺回復:面試,可以獲取我整理的 Java 系列面試題和答案,非常齊全。






          關(guān)注Java技術(shù)??锤喔韶?/strong>



          獲取 Spring Boot 實戰(zhàn)筆記!
          瀏覽 49
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  牛牛AV国产一区二区 | 日韩一级片在线看 | 波多野吉衣网站 | 天天摸天天碰成人免费视频 | 午夜AV福利电影 |