日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot整合ShardingSphere5.x實現(xiàn)數(shù)據(jù)加解密功能

準備環(huán)境

  • 添加依賴?

org.apache.shardingsphere
shardingsphere-jdbc-core
${shardingsphere.version}


com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.version}


com.baomidou
mybatis-plus-generator
${mybatis-plus.version}
  • 數(shù)據(jù)表users

  1. pwd:明文字段
  2. pwd_clipher:密文字段
  3. assisted_query_pwd:查詢輔助列
  • 配置文件

application.yml配置文件(Springboot)?

創(chuàng)新互聯(lián)服務(wù)項目包括合江網(wǎng)站建設(shè)、合江網(wǎng)站制作、合江網(wǎng)頁制作以及合江網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,合江網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到合江省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

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?serverTimeznotallow=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)一般都會吧這個邏輯列和實際物理列名一致)
plainColumn: pwd #實際物理列名
cipherColumn: pwd_cipher #加密后的列名
encryptorName: pwd_encryptor #加密列使用的加密算法(對應(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í)行時打印SQL
props:
sql-show: true

有了上面配置后,接下來就可以進行相應(yīng)的CRUD操作了。

CRUD操作

實體對象?

@TableName("users")
public class Users {

@TableId(type = IdType.ASSIGN_ID)
private Long id;
private String name;
@TableField("id_no")
private String idNo ;
private Integer age;
private String email;
private String pwd ;
}

Mapper類?

public interface UsersMapper extends BaseMapper {

}

測試類?

@SpringBootTest
public class UserMapperTest {

@Resource
private UsersMapper usersMapper ;
@Resource
private IUsersService us ;
@Resource
private List dataSources ;

@Test
public void testUserList() {
QueryWrapper 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) ;
}


}

測試結(jié)果

數(shù)據(jù)源及查詢輔助列

  • 數(shù)據(jù)源配置

在config.yaml文件中我們配置了連接池信息,但是實際沒有生效。如上配置的最小連接數(shù)是10,最大是200,但是實際打印都成了默認值都是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

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

  • 輔助查詢列

輔助查詢列會根據(jù)你的配置是否使用輔助列,當沒有配置輔助查詢列時,執(zhí)行SQL如下:

使用的是加密列進行查詢了

當配置了輔助查詢列后:

使用的是輔助列查詢。


分享標題:SpringBoot整合ShardingSphere5.x實現(xiàn)數(shù)據(jù)加解密功能
URL鏈接:http://www.dlmjj.cn/article/dpjgsoi.html