springboot 整合druid資料庫密碼加密功能的實現程式碼
阿新 • • 發佈:2021-01-08
在之前給大家介紹過Springboot Druid 自定義加密資料庫密碼的幾種方案,感興趣的朋友可以點選檢視下,今天通過本文給大家介紹springboot 整合druid資料庫密碼加密功能,具體內容如下所示:
1.依賴引入
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency>
2.密碼加密處理
public static void main(String[] args) throws Exception { String password = "Aq6vD!puWbk"; System.out.println("明文密碼: " + password); String[] keyPair = ConfigTools.genKeyPair(512); //私鑰 String privateKey = keyPair[0]; //公鑰 String publicKey = keyPair[1]; //用私鑰加密後的密文 password = ConfigTools.encrypt(privateKey,password); System.out.println("privateKey:" + privateKey); System.out.println("publicKey:" + publicKey); System.out.println("password:" + password); String decryptPassword = ConfigTools.decrypt(publicKey,password); System.out.println("解密後:" + decryptPassword); }
3.yml配置檔案修改
connectionProperties需要注意,其他版本有connection-properties和connect-properties,注意區分
datasource: type: com.alibaba.druid.pool.DruidDataSource # 特別注意:java 9以後需要將com.mysql.jdbc.Driver 改為 com.mysql.cj.jdbc.Driver即可 # 否則報錯:Loading class `com.mysql.jdbc.Driver'. This is deprecated. driver-class-name: com.mysql.cj.jdbc.Driver druid: #基本屬性 url: jdbc:mysql://1******:3306/**?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai username: admin password: Qh0VAjlS/LVbsAFSAFsdf24jJ7rggMNsnvJex3x1mkUKxPd2bofuAR6DtjCV20M4n2DWc5SLZmkzgjvG3Elx1g== #此處需要注意,其他版本有connection-properties和connect-properties,注意區分 connectionProperties: config.decrypt=true;config.decrypt.key=${publicKey}; filter: config: enabled: true # 啟動ConfigFilter #配置初始化大小/最小/最大[僅用於測試,生產環境需要修改] initial-size: 5 min-idle: 5 max-active: 20 #獲取連線等待超時時間 max-wait: 60000 #間隔多久進行一次檢測,檢測需要關閉的空閒連線 time-between-eviction-runs-millis: 10000 #一個連線在池中最小生存的時間 min-evictable-idle-time-millis: 300000 #指定獲取連線時連線校驗的sql查詢語句 validation-query: SELECT 'x' #驗證連線的有效性 test-while-idle: true #獲取連線時候驗證,會影響效能(不建議true) test-on-borrow: false #開啟PSCache,並指定每個連線上PSCache的大小。oracle設為true,mysql設為false。分庫分表較多推薦設定為false pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20 publicKey: MFwwDQYJKoZIhafwqfDSAwAwSAJBAIG3LgXwadfgferwbWdkGNDzgrjfSWfrBjJ2X+m9lajH7yGPeE/vLs4hdtr1RCITBKJeevZpwZ0DBLctVS6Dc0CAwEAAQ==
到此這篇關於springboot 整合druid資料庫密碼加密功能的實現程式碼的文章就介紹到這了,更多相關springboot 整合druid密碼加密內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!