当前位置:   article > 正文

SpringBoot整合ShardingSphere5.x实现数据加解密功能_spring项目引入sharding-jdbc加密

spring项目引入sharding-jdbc加密

环境:Springboot2.6.14 + ShardingSphere5.3.0


准备环境

  • 添加依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc-core</artifactId>
  4. <version>${shardingsphere.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>mybatis-plus-boot-starter</artifactId>
  9. <version>${mybatis-plus.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.baomidou</groupId>
  13. <artifactId>mybatis-plus-generator</artifactId>
  14. <version>${mybatis-plus.version}</version>
  15. </dependency>
  • 数据表users

  1. pwd:明文字段

  2. pwd_clipher:密文字段

  3. assisted_query_pwd:查询辅助列

  • 配置文件

application.yml配置文件(Springboot)

  1. spring:
  2. datasource:
  3. driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver
  4. url: jdbc:shardingsphere:classpath:config.yaml
  5. name: EncryptHikariCP
  6. ---
  7. mybatis-plus:
  8. configuration:
  9. mapUnderscoreToCamelCase: true
  10. mapperLocations: classpath*:/mapper/**/*.xml
  11. typeAliasesPackage: com.pack

config.yaml配置文件(ShardingSphere)

  1. #数据源配置
  2. dataSources:
  3. ds1:
  4. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  5. driverClassName: com.mysql.cj.jdbc.Driver
  6. jdbcUrl: jdbc:mysql://localhost:3306/testjpa?serverTimezone=GMT%2B8&useSSL=false
  7. username: root
  8. password: 123123
  9. minimumIdle: 10
  10. maximumPoolSize: 200
  11. autoCommit: true
  12. idleTimeout: 30000
  13. poolName: MasterHikariCP
  14. maxLifetime: 1800000
  15. connectionTimeout: 30000
  16. connectionTestQuery: SELECT 1
  17. #规则配置
  18. rules:
  19. - !ENCRYPT #加解密相关配置
  20. tables:
  21. users:
  22. columns:
  23. pwd: #逻辑列(如果是老系统一般都会吧这个逻辑列和实际物理列名一致)
  24. plainColumn: pwd #实际物理列名
  25. cipherColumn: pwd_cipher #加密后的列名
  26. encryptorName: pwd_encryptor #加密列使用的加密算法(对应下面的配置)
  27. #assistedQueryColumn: assisted_query_pwd
  28. #assistedQueryEncryptorName: assisted_encryptor
  29. queryWithCipherColumn: true
  30. encryptors:
  31. pwd_encryptor:
  32. type: SM4
  33. props:
  34. sm4-key: aaaabbbbccccdddd1111222233334444
  35. sm4-mode: ECB
  36. sm4-iv: aabbccddeeffgghh
  37. sm4-padding: PKCS7Padding
  38. assisted_encryptor:
  39. type: SM3
  40. props:
  41. sm3-salt: aaaabbbb
  42. #执行时打印SQL
  43. props:
  44. sql-show: true

有了上面配置后,接下来就可以进行相应的CRUD操作了。

CRUD操作

实体对象

  1. @TableName("users")
  2. public class Users {
  3. @TableId(type = IdType.ASSIGN_ID)
  4. private Long id;
  5. private String name;
  6. @TableField("id_no")
  7. private String idNo ;
  8. private Integer age;
  9. private String email;
  10. private String pwd ;
  11. }

Mapper类

  1. public interface UsersMapper extends BaseMapper<Users> {
  2. }

测试类

  1. @SpringBootTest
  2. public class UserMapperTest {
  3. @Resource
  4. private UsersMapper usersMapper ;
  5. @Resource
  6. private IUsersService us ;
  7. @Resource
  8. private List<DataSource> dataSources ;
  9. @Test
  10. public void testUserList() {
  11. QueryWrapper<Users> queryWrapper = new QueryWrapper<>() ;
  12. queryWrapper.eq("pwd", "999999") ;
  13. System.out.println(this.usersMapper.selectList(queryWrapper)) ;
  14. }
  15. @Test
  16. public void testSave() {
  17. Users user = new Users() ;
  18. user.setAge(99) ;
  19. user.setEmail("99999@qq.com") ;
  20. user.setIdNo("999999") ;
  21. user.setName("久久") ;
  22. user.setPwd("999999") ;
  23. this.usersMapper.insert(user) ;
  24. }
  25. }

测试结果

数据源及查询辅助列

  • 数据源配置

在config.yaml文件中我们配置了连接池信息,但是实际没有生效。如上配置的最小连接数是10,最大是200,但是实际打印都成了默认值都是10。最后修改连接池配置方式如下:

  1. spring:
  2. datasource:
  3. driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver
  4. url: jdbc:shardingsphere:classpath:config.yaml
  5. name: EncryptHikariCP
  6. hikari:
  7. minimumIdle: 10
  8. maximumPoolSize: 200
  9. autoCommit: true
  10. idleTimeout: 30000
  11. poolName: BaseHikariCP
  12. maxLifetime: 1800000
  13. connectionTimeout: 30000
  14. connectionTestQuery: SELECT 1

如上配置后连接池才正常。

  • 辅助查询列

辅助查询列会根据你的配置是否使用辅助列,当没有配置辅助查询列时,执行SQL如下:

使用的是加密列进行查询了

当配置了辅助查询列后:

使用的是辅助列查询。

完毕!!!

 图片

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/756830
推荐阅读
相关标签
  

闽ICP备14008679号