SpringBoot整合ShardingSphere5.x實(shí)現(xiàn)數(shù)據(jù)加解密功能(最新推薦)
環(huán)境:Springboot2.6.14 + ShardingSphere5.3.0
準(zhǔn)備環(huán)境
添加依賴(lài)
<dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core</artifactId> <version>${shardingsphere.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus.version}</version> </dependency>
數(shù)據(jù)表users
pwd:明文字段
pwd_clipher:密文字段
assisted_query_pwd:查詢(xún)輔助列
配置文件
application.yml配置文件(Springboot)
spring: datasource: driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver url: jdbc:shardingsphere:classpath:config.yaml name: EncryptHikariCP --- mybatis-plus: configuration: mapUnderscoreToCamelCase: true mapperLocations: classpath*:/mapper/**/*.xml typeAliasesPackage: com.pack
config.yaml配置文件(ShardingSphere)
#數(shù)據(jù)源配置 dataSources: ds1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/testjpa?serverTimezone=GMT%2B8&useSSL=false username: root password: 123123 minimumIdle: 10 maximumPoolSize: 200 autoCommit: true idleTimeout: 30000 poolName: MasterHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1 #規(guī)則配置 rules: - !ENCRYPT #加解密相關(guān)配置 tables: users: columns: pwd: #邏輯列(如果是老系統(tǒng)一般都會(huì)吧這個(gè)邏輯列和實(shí)際物理列名一致) plainColumn: pwd #實(shí)際物理列名 cipherColumn: pwd_cipher #加密后的列名 encryptorName: pwd_encryptor #加密列使用的加密算法(對(duì)應(yīng)下面的配置) #assistedQueryColumn: assisted_query_pwd #assistedQueryEncryptorName: assisted_encryptor queryWithCipherColumn: true encryptors: pwd_encryptor: type: SM4 props: sm4-key: aaaabbbbccccdddd1111222233334444 sm4-mode: ECB sm4-iv: aabbccddeeffgghh sm4-padding: PKCS7Padding assisted_encryptor: type: SM3 props: sm3-salt: aaaabbbb #執(zhí)行時(shí)打印SQL props: sql-show: true
有了上面配置后,接下來(lái)就可以進(jìn)行相應(yīng)的CRUD操作了。
CRUD操作
實(shí)體對(duì)象
#數(shù)據(jù)源配置 dataSources: ds1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/testjpa?serverTimezone=GMT%2B8&useSSL=false username: root password: 123123 minimumIdle: 10 maximumPoolSize: 200 autoCommit: true idleTimeout: 30000 poolName: MasterHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1 #規(guī)則配置 rules: - !ENCRYPT #加解密相關(guān)配置 tables: users: columns: pwd: #邏輯列(如果是老系統(tǒng)一般都會(huì)吧這個(gè)邏輯列和實(shí)際物理列名一致) plainColumn: pwd #實(shí)際物理列名 cipherColumn: pwd_cipher #加密后的列名 encryptorName: pwd_encryptor #加密列使用的加密算法(對(duì)應(yīng)下面的配置) #assistedQueryColumn: assisted_query_pwd #assistedQueryEncryptorName: assisted_encryptor queryWithCipherColumn: true encryptors: pwd_encryptor: type: SM4 props: sm4-key: aaaabbbbccccdddd1111222233334444 sm4-mode: ECB sm4-iv: aabbccddeeffgghh sm4-padding: PKCS7Padding assisted_encryptor: type: SM3 props: sm3-salt: aaaabbbb #執(zhí)行時(shí)打印SQL props: sql-show: true
Mapper類(lèi)
public interface UsersMapper extends BaseMapper<Users> { }
測(cè)試類(lèi)
@SpringBootTest public class UserMapperTest { @Resource private UsersMapper usersMapper ; @Resource private IUsersService us ; @Resource private List<DataSource> dataSources ; @Test public void testUserList() { QueryWrapper<Users> queryWrapper = new QueryWrapper<>() ; queryWrapper.eq("pwd", "999999") ; System.out.println(this.usersMapper.selectList(queryWrapper)) ; } @Test public void testSave() { Users user = new Users() ; user.setAge(99) ; user.setEmail("99999@qq.com") ; user.setIdNo("999999") ; user.setName("久久") ; user.setPwd("999999") ; this.usersMapper.insert(user) ; } }
測(cè)試結(jié)果
數(shù)據(jù)源及查詢(xún)輔助列
數(shù)據(jù)源配置
在config.yaml文件中我們配置了連接池信息,但是實(shí)際沒(méi)有生效。如上配置的最小連接數(shù)是10,最大是200,但是實(shí)際打印都成了默認(rèn)值都是10。最后修改連接池配置方式如下:
spring: datasource: driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver url: jdbc:shardingsphere:classpath:config.yaml name: EncryptHikariCP hikari: minimumIdle: 10 maximumPoolSize: 200 autoCommit: true idleTimeout: 30000 poolName: BaseHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1
如上配置后連接池才正常。
輔助查詢(xún)列
輔助查詢(xún)列會(huì)根據(jù)你的配置是否使用輔助列,當(dāng)沒(méi)有配置輔助查詢(xún)列時(shí),執(zhí)行SQL如下:
使用的是加密列進(jìn)行查詢(xún)了
當(dāng)配置了輔助查詢(xún)列后:
使用的是輔助列查詢(xún)。
到此這篇關(guān)于SpringBoot整合ShardingSphere5.x實(shí)現(xiàn)數(shù)據(jù)加解密功能的文章就介紹到這了,更多相關(guān)SpringBoot整合ShardingSphere5.x內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫(kù)分表的方案
- SpringBoot?整合?ShardingSphere4.1.1實(shí)現(xiàn)分庫(kù)分表功能
- springboot如何使用yml文件方式配置shardingsphere
- SpringBoot3和ShardingSphere5框架實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
- SpringBoot+ShardingSphereJDBC實(shí)現(xiàn)讀寫(xiě)分離詳情
- SpringBoot如何配置數(shù)據(jù)庫(kù)主從shardingsphere
相關(guān)文章
JavaWeb禁止瀏覽器緩存當(dāng)前Web頁(yè)面的方法
所謂瀏覽器緩存,是指當(dāng)?shù)谝淮卧L(fǎng)問(wèn)網(wǎng)頁(yè)時(shí),瀏覽器會(huì)將這些網(wǎng)頁(yè)緩存到本地,當(dāng)下一次再訪(fǎng)問(wèn)這些被緩存的網(wǎng)頁(yè)時(shí),瀏覽器就會(huì)直接從本地讀取這些網(wǎng)頁(yè)的內(nèi)容,而無(wú)需再?gòu)木W(wǎng)絡(luò)上獲取2017-11-11如何通過(guò)Java實(shí)現(xiàn)修改視頻分辨率
Java除了可以修改圖片的分辨率,還可以實(shí)現(xiàn)修改視頻的分辨率,這篇文章就將帶大家學(xué)習(xí)如果編寫(xiě)這一工具類(lèi),感興趣的同學(xué)可以了解一下2021-12-12詳解Springboot中的異步、定時(shí)、郵件任務(wù)
這篇文章主要介紹了Springboot中的異步、定時(shí)、郵件任務(wù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列
這篇文章主要為大家詳細(xì)介紹了Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10springboot中@component注解的使用實(shí)例
這篇文章主要介紹了springboot中@component注解的使用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java隨機(jī)密碼生成并和郵箱、手機(jī)號(hào)匹配
這篇文章主要介紹了Java隨機(jī)密碼生成并和郵箱、手機(jī)號(hào)匹配的相關(guān)資料,需要的朋友可以參考下2016-01-01MyBatis-Plus使用sl4j日志打印SQL的代碼詳解
以下是關(guān)于使用 Spring Boot 起始器替換 slf4j-api 和 logback 依賴(lài)的詳細(xì)步驟和注意事項(xiàng),包括 MyBatis-Plus 的默認(rèn)日志級(jí)別信息,需要的朋友可以參考下2024-10-10Mybatis-Plus更新數(shù)據(jù)忽略null值問(wèn)題
本文主要介紹了Mybatis-Plus更新數(shù)據(jù)忽略null值問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02