Spring Boot + Sharding-JDBC实现读写分离和数据脱敏
使用官网上的读写分离配置+脱敏配置,发现脱敏没有效果,
尝试了手动配置数据源,测试有效。
注:如果脱敏字段的值为null时,脱敏会报空指针异常。
1.pom文件
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2.添加property配置
datasource.master.type=org.apache.commons.dbcp2.BasicDataSource
datasource.master.driver-class-name=com.mysql.jdbc.Driver
datasource.master.url=jdbc:mysql://192.168.1.196:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
datasource.master.username=
datasource.master.password=
datasource.slave0.type=org.apache.commons.dbcp2.BasicDataSource
datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
datasource.slave0.url=jdbc:mysql://192.168.1.195:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
datasource.slave0.username=
datasource.slave0.password=
#加密方式 自带AES和MD5 可自定义
encrypt.type=AES
#加密字段,多个用,分隔,格式:表名.字段名
encrypt.qualifiedColumns=
#AES加密所需密钥
encrypt.aes.key.value=
3.配置数据源
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptorRuleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
impo