SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密相關(guān)代碼
一、說(shuō)明
我們?cè)趯慡pringboot項(xiàng)目時(shí)候,配置文件中需要配置數(shù)據(jù)庫(kù)連接,用戶名和密碼都是明文配置的。這樣做很不安全,容易密碼泄露。
二、加密方案
1、加密方案有好多種,下來(lái)介紹一種本人用的,比較簡(jiǎn)單的加密方法。
2、使用說(shuō)明:
使用密碼加密工具類,生成加密后的字符串,配置到你的項(xiàng)目配置文件中,項(xiàng)目啟動(dòng)后,springboot項(xiàng)目會(huì)根據(jù)你寫的解密方法去自行解密,從而鏈接到你的數(shù)據(jù)庫(kù)。
三、相關(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: true2、密碼加密工具類
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ù)庫(kù)配置類
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ù)庫(kù)密碼
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項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密的文章就介紹到這了,更多相關(guān)SpringBoot配置數(shù)據(jù)庫(kù)密碼加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系
這篇文章主要為大家詳細(xì)介紹了Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
@RequestBody時(shí)第二個(gè)字母大寫,映射不到的解決
這篇文章主要介紹了@RequestBody時(shí)第二個(gè)字母大寫,映射不到的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
這篇文章主要介紹了多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
這篇文章主要介紹了SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
用Java連接sqlserver數(shù)據(jù)庫(kù)時(shí)候幾個(gè)jar包的區(qū)別分析
這篇文章主要介紹了用Java連接sqlserver數(shù)據(jù)庫(kù)時(shí)候幾個(gè)jar包的區(qū)別分析,需要的朋友可以參考下2014-10-10
詳解Java如何實(shí)現(xiàn)百萬(wàn)數(shù)據(jù)excel導(dǎo)出功能
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)百萬(wàn)數(shù)據(jù)excel導(dǎo)出功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02
springboot2.2 集成 activity6實(shí)現(xiàn)請(qǐng)假流程(示例詳解)
這篇文章主要介紹了springboot2.2 集成 activity6實(shí)現(xiàn)請(qǐng)假完整流程示例詳解,本文通過(guò)示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java實(shí)現(xiàn)冒泡排序算法及對(duì)其的簡(jiǎn)單優(yōu)化示例
這篇文章主要介紹了Java實(shí)現(xiàn)冒泡排序算法及對(duì)其的簡(jiǎn)單優(yōu)化示例,冒泡排序的最差時(shí)間復(fù)雜度為O(n^2),最優(yōu)時(shí)間復(fù)雜度為O(n),存在優(yōu)化的余地,需要的朋友可以參考下2016-05-05
身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn)
這篇文章主要介紹了身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn),本文講解了18身份證號(hào)碼的結(jié)構(gòu)、根據(jù)17位數(shù)字本體碼獲取最后一位校驗(yàn)碼程序?qū)嵗葍?nèi)容,需要的朋友可以參考下2015-06-06

