java 数据库数据脱敏_Sharding-JDBC-数据脱敏

数据脱敏

该章节主要介绍如何使用数据脱敏功能,如何进行相关配置。数据脱敏功能即可与数据分片功能共同使用,又可作为单独功能组件,独立使用。 与数据分片功能共同使用时,会创建ShardingDataSource;单独使用时,会创建EncryptDataSource来完成数据脱敏功能。

不使用Spring

引入Maven依赖

org.apache.shardingsphere

sharding-jdbc-core

${sharding-sphere.version}

基于Java编码的规则配置

// 配置数据源

BasicDataSource dataSource = new BasicDataSource();

dataSource.setDriverClassName("com.mysql.jdbc.Driver");

dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/encrypt");

dataSource.setUsername("root");

dataSource.setPassword("");

// 配置脱敏规则

Properties props = new Properties();

props.setProperty("aes.key.value", "123456");

EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("aes", "t_order.order_id", new Properties());

EncryptRuleConfiguration ruleConfiguration = new EncryptRuleConfiguration();

ruleConfiguration.getEncryptorRuleConfigs().put("order_encryptor", encryptorConfig);

// 获取数据源对象

DataSource dataSource = EncryptDataSourceFactory.createDataSource(dataSource, ruleConfiguration);

基于Yaml的规则配置

或通过Yaml方式配置,与以上配置等价:

dataSource: !!org.apache.commons.dbcp2.BasicDataSource

driverClassName: com.mysql.jdbc.Driver

jdbcUrl: jdbc:mysql://127.0.0.1:3306/encrypt?serverTimezone=UTC&useSSL=false

username: root

password:

encryptRule:

encryptors:

order_encryptor:

type: aes

qualifiedColumns: t_order.user_id

props:

aes.key.value: 123456

DataSource dataSource = YamlEncryptDataSourceFactory.createDataSource(yamlFile);

使用Spring

引入Maven依赖

org.apache.shardingsphere

sharding-jdbc-spring-boot-starter

${sharding-sphere.version}

org.apache.shardingsphere

sharding-jdbc-spring-namespace

${sharding-sphere.version}

基于Spring boot的规则配置

spring.shardingsphere.datasource.name=ds

spring.shardingsphere.datasource.ds.type=org.apache.commons.dbcp2.BasicDataSource

spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driver

spring.shardingsphere.datasource.ds.url=jdbc:mysql://127.0.0.1:3306/encrypt?serverTimezone=UTC&useSSL=false

spring.shardingsphere.datasource.ds.username=root

spring.shardingsphere.datasource.ds.password=

spring.shardingsphere.datasource.ds.max-total=100

spring.shardingsphere.encrypt.encryptors.order_encrypt.type=aes

spring.shardingsphere.encrypt.encryptors.order_encrypt.qualifiedColumns=t_order.user_id

spring.shardingsphere.encrypt.encryptors.order_encrypt.props.aes.key.value=123456

基于Spring命名空间的规则配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"

xmlns:bean="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://shardingsphere.apache.org/schema/shardingsphere/encrypt

http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util.xsd">

123456

在Spring中使用DataSource

直接通过注入的方式即可使用DataSource,或者将DataSource配置在JPA、Hibernate或MyBatis中使用。

@Resource

private DataSource dataSource;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值