SpringBoot項目配置數(shù)據(jù)庫密碼加密相關(guān)代碼
一、說明
我們在寫Springboot項目時候,配置文件中需要配置數(shù)據(jù)庫連接,用戶名和密碼都是明文配置的。這樣做很不安全,容易密碼泄露。
二、加密方案
1、加密方案有好多種,下來介紹一種本人用的,比較簡單的加密方法。
2、使用說明:
使用密碼加密工具類,生成加密后的字符串,配置到你的項目配置文件中,項目啟動后,springboot項目會根據(jù)你寫的解密方法去自行解密,從而鏈接到你的數(shù)據(jù)庫。
三、相關(guān)代碼
1、application.yml
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/patient?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: test password: oiWRKCcmZH/pQes5KH03kgVSHza7OK/G jpa: hibernate: ddl-auto: update show-sql: true
2、密碼加密工具類
package com.jianqi.HL7Service.config; import org.jasypt.properties.PropertyValueEncryptionUtils; import org.jasypt.util.text.BasicTextEncryptor; public final class JasyptEncryptorUtils { private static final String salt = "test666"; private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); static { basicTextEncryptor.setPassword(salt); } private JasyptEncryptorUtils(){} /** * 明文加密 * @param plaintext * @return */ public static String encode(String plaintext){ System.out.println("明文字符串:" + plaintext); String ciphertext = basicTextEncryptor.encrypt(plaintext); return ciphertext; } /** * 解密 * @param ciphertext * @return */ public static String decode(String ciphertext){ ciphertext = "ENC(" + ciphertext + ")"; if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)){ String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext,basicTextEncryptor); return plaintext; } System.out.println("解密失敗"); return ""; } public static void main(String[] args) { // 需要加密的明文 String plaintext = "patient113"; // 加密明文 String encryptedText = JasyptEncryptorUtils.encode(plaintext); System.out.println("加密后字符串:" + encryptedText); // 解密密文 String decryptedText = JasyptEncryptorUtils.decode(encryptedText); System.out.println("解密后的字符串:" + decryptedText); } }
3、數(shù)據(jù)庫配置類
package com.jianqi.HL7Service.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.boot.jdbc.DataSourceBuilder; import javax.sql.DataSource; @Configuration @EnableJpaRepositories(basePackages = "com.jianqi.HL7Service.repository") @EnableTransactionManagement public class DatabaseConfig { @Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String dbUsername; @Value("${spring.datasource.password}") private String dbEncryptedPassword; @Bean public DataSource dataSource() { // 使用 JasyptEncryptorUtils 解密數(shù)據(jù)庫密碼 String dbPassword = JasyptEncryptorUtils.decode(dbEncryptedPassword); return DataSourceBuilder.create() .url(dbUrl) .username(dbUsername) .password(dbPassword) .build(); } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setDataSource(dataSource()); return transactionManager; } }
總結(jié)
到此這篇關(guān)于SpringBoot項目配置數(shù)據(jù)庫密碼加密的文章就介紹到這了,更多相關(guān)SpringBoot配置數(shù)據(jù)庫密碼加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系
這篇文章主要為大家詳細介紹了Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10多個sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的實現(xiàn)方法
這篇文章主要介紹了多個sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03SpringBoot之使用Redis實現(xiàn)分布式鎖(秒殺系統(tǒng))
這篇文章主要介紹了SpringBoot之使用Redis實現(xiàn)分布式鎖(秒殺系統(tǒng)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04用Java連接sqlserver數(shù)據(jù)庫時候幾個jar包的區(qū)別分析
這篇文章主要介紹了用Java連接sqlserver數(shù)據(jù)庫時候幾個jar包的區(qū)別分析,需要的朋友可以參考下2014-10-10詳解Java如何實現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-02-02springboot2.2 集成 activity6實現(xiàn)請假流程(示例詳解)
這篇文章主要介紹了springboot2.2 集成 activity6實現(xiàn)請假完整流程示例詳解,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Java實現(xiàn)冒泡排序算法及對其的簡單優(yōu)化示例
這篇文章主要介紹了Java實現(xiàn)冒泡排序算法及對其的簡單優(yōu)化示例,冒泡排序的最差時間復(fù)雜度為O(n^2),最優(yōu)時間復(fù)雜度為O(n),存在優(yōu)化的余地,需要的朋友可以參考下2016-05-05