【sharding-jdbc】spring boot 集成 sharding-jdbc 实现数据脱敏

应实际业务需求,需要对业务部分数据进行脱敏处理。sharding-jdbc 框架能够实现此功能,集成此框架到项目中去。
集成sharding-jdbc前建议仔细阅读官网相关文档shardingsphere 官网shardingsphere 数据脱敏官方博文(重要)shardingsphere github地址

步骤

  • 引入相关依赖
<sharding-sphere.version>4.0.0-RC2</sharding-sphere.version>
   <!-- sharding-jdbc -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>${sharding-sphere.version}</version>
        </dependency>
  • 进行数据脱敏配置
spring:
  shardingsphere:
    datasource:
      name: ds
      ds:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.p6spy.engine.spy.P6SpyDriver
        url: jdbc:p6spy:mysql://0.0.0.0:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
        username: root
        password: pwd
        initial-size: 10
        max-active: 10
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: SELECT 1
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        filter:
          stat:
            log-slow-sql: true
            slow-sql-millis: 1000
            merge-sql: true
          wall:
            config:
              multi-statement-allow: true
    encrypt:
      encryptors:
        encryptor_aes:
          type: aes
          props:
            aes.key.value: 123456
      tables:
        s_u_user:
          columns:
            remark:
#              plainColumn: user_decrypt
              cipherColumn: remark
#              assistedQueryColumn: user_assisted
              encryptor: encryptor_aes
    props:
      sql:
        show: true
      query:
        with:
          cipher:
            column: true

说明:

image.png

如何理解用户想使用哪个列进行SQL编写(logicColumn)?
我们可以从Encrypt-JDBC存在的意义来理解。Encrypt-JDBC最终目的是希望屏蔽底层对数据的脱敏处理,也就是说我们不希望用户知道数据是如何被加解密的、如何将明文数据存储到plainColumn,将密文数据存储到cipherColumn。换句话说,我们不希望用户知道plainColumn和cipherColumn的存在和使用。所以,我们需要给用户提供一个概念意义上的列,这个列可以脱离底层数据库的真实列,它可以是数据库表里的一个真实列,也可以不是,从而使得用户可以随意改变底层数据库的plainColumn和cipherColumn的列名。或者删除plainColumn,选择永远不再存储明文,只存储密文。只要用户的SQL面向这个逻辑列进行编写,并在脱敏规则里给出logicColumn和plainColumn、cipherColumn之间正确的映射关系即可。

遇到的问题配置逻辑列时遇到问题,因项目中使用另一个框架mybatis plus,使用了自动驼峰命名规则映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。在sharding-jdbc 配置 logicColumn逻辑列时,项目会产生报错。因此在使用时,尽量避免在此中字段列中使用。
上述问题再次测试时,没有复现。等再次遇到再更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT从业者的职业生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值