shardingsphere-jdbc之JPA 敏感数据加密
·
介绍
基于shardingsphere-jdbc 5.1.0 对敏感数据进行加密
默认加密算法
- MD5 加密算法
- AES 加密算法
- RC4 加密算法
- SM3 加密算法
- SM4 加密算法
1. maven项目依赖
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
2.application.yml配置
spring:
application:
name: jdbc-jpa-encrypt
profiles:
include: jdbc
jpa:
show-sql: true
hibernate:
ddl-auto: none
naming:
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
database-platform: org.hibernate.dialect.MySQL8Dialect
properties:
hibernate.enable_lazy_load_no_trans: true
logging:
file:
name: logs/${spring.application.name}.log
level:
org.springframework: info
com.lance.sharding.encrypt: debug
3.application-jdbc.yml配置
spring:
shardingsphere:
datasource:
ds:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/bbs_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
password: li123456
username: root
names: ds
rules:
encrypt:
encryptors:
name-encryptor:
props:
aes-key-value: hello@aes.username
type: AES
pwd-encryptor:
props:
aes-key-value: hello@aes.password
type: AES
tables:
t_user:
columns:
pwd:
cipher-column: pwd
encryptor-name: pwd-encryptor
username:
cipher-column: username
encryptor-name: name-encryptor
props:
sql-show: true
query-with-cipher-comlum: true
4.测试Sql脚本
CREATE TABLE t_user
(
user_id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(32),
pwd VARCHAR(32),
`status` tinyint NULL DEFAULT NULL,
`creator` varchar(32) NULL DEFAULT NULL,
`create_time` datetime NULL DEFAULT NULL,
`updater` varchar(32) NULL DEFAULT NULL,
`update_time` datetime NULL DEFAULT NULL,
PRIMARY KEY (user_id)
);
5.单元测试Test
class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Test
@Disabled
void save() {
IntStream.range(0, 20).forEach(i -> {
UserEntity user = new UserEntity();
user.setUsername("user00" + i);
user.setPwd("pwd@" + i);
user.setCreator(user.getUsername());
user.setUpdater(user.getUpdater());
userRepository.save(user);
});
}
@Test
@Disabled
void findByUsername() {
String username = "user001";
UserEntity user = userRepository.findByUsername(username);
log.info("===>{}", user);
}
}
6.项目完整地址
更多推荐
所有评论(0)