当前位置: 代码网 > it编程>编程语言>Java > 使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

2025年06月10日 Java 我要评论
在真实业务场景中,数据库中经常需要存储某些客户的关键性敏感信息如:身份证号、银行卡号、姓名、手机号码等,此类信息按照合规要求,通常需要实现加密存储以满足合规要求。痛点一:通常的解决方案是书写sql的时

在真实业务场景中,数据库中经常需要存储某些客户的关键性敏感信息如:身份证号、银行卡号、姓名、手机号码等,此类信息按照合规要求,通常需要实现加密存储以满足合规要求。 

痛点一:

通常的解决方案是书写sql的时候,把对应的加密字段手动进行加密再进行插入,在查询的时候使用之前再手动进行解密。此方法固然可行,但是使用起来非常不便捷且繁琐,使得日常的业务开发与存储合规的细节紧耦合 

痛点二:

对于一些为了快速上线而一开始没有实现合规脱敏的系统,如何比较快速的使得已有业务满足合规要求的同时,尽量减少对原系统的改造。(通常的这个过程至少包括:1.新增脱敏列的存储 2.同时做数据迁移 3.业务的代码做兼容逻辑等)。
apache shardingsphere下面存在一个数据脱敏模块,此模块集成的常用的数据脱敏的功能。其基本原理是对用户输入的sql进行解析拦截,并依靠用户的脱敏配置进行sql的改写,从而实现对原文字段的加密及加密字段的解密。最终实现对用户无感的加解密存储、查询。 

脱敏配置quick start——spring 显示配置:

以下介绍基于spring如何快速让系统支持脱敏配置。 

1.引入依赖

<!-- for spring namespace -->
<dependency>
    <groupid>org.apache.shardingsphere</groupid>
    <artifactid>sharding-jdbc-spring-namespace</artifactid>
    <version>${sharding-sphere.version}</version>
</dependency>

2.创建脱敏配置规则对象

在创建数据源之前,需要准备一个 encryptruleconfiguration进行脱敏的配置,以下是一个例子,对于同一个数据源里两张表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;
}

说明:

  • 创建 encryptcolumnruleconfiguration 的时候有四个参数,前两个参数分表叫plaincolumnciphercolumn,其意思是数据库存储里面真实的两个列(名文列、脱敏列),对于新的系统,只需要设置脱敏列即可,所以以上示例为plaincolumn为””。
  • 创建encrypttableruleconfiguration 的时候需要传入一个map,这个map存的value即#1中说明的encryptcolumnruleconfiguration ,而其key则是一个逻辑列,对于新系统,此逻辑列即为真实的脱敏列。sharding shpere在拦截到sql改写的时候,会按照用户的配置,把逻辑列映射为名文列或者脱敏列(默认)如下的示例

3.使用sharding sphere的数据源进行管理

把原始的数据源包装一层

@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实现数据脱敏的文章就介绍到这了,更多相关springboot sharding sphere数据脱敏内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com