springboot使用jasypt對配置文件加密加密數(shù)據(jù)庫連接的操作代碼
springboot使用jasypt對配置文件加密
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>springboot配置
jasypt:
encryptor:
password: saltValue #salt值,密文加鹽
spring:
datasource: # 數(shù)據(jù)庫鏈接
db1:
jdbc-url: jdbc:mysql://x.x.x.x:3306/db_test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root #也可以加密用戶名,依然是ENC()格式,這里沒有進行加密
password: ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+) #加密了密碼,ENC()括號內(nèi)為密文
driver-class-name: com.mysql.cj.jdbc.Driver
mapper-locations: classpath*:mapper/otcmapper/*.xml啟動類添加注解:@EnableEncryptableProperties
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEncryptableProperties
@EnableScheduling
//@EnableAsync
public class SpBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpBatchApplication.class, args);
}
}通過明文獲取加密的值 cmd在自己的maven倉庫目錄下執(zhí)行命令,(要保證依賴下載下來了)
解釋:input:文字的明文password:加密的鹽值(可隨意,必須=jasypt:encryptor:password: saltValue)algorithm:PBEWithMD5AndDES(默認(rèn)算法)
java -cp org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密碼明文" password=saltValue algorithm=PBEWithMD5AndDES
執(zhí)行后輸出結(jié)果:OUTPUT就是密文了,把密文替換yml的屬性值就行
ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)
----ARGUMENTS------------------- algorithm: PBEWithMD5AndDES input: 密碼明文 password: saltValue ----OUTPUT---------------------- OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+
啟動springboot就會自動解密了
通過密文和鹽值解密得到明文
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=saltValue algorithm=PBEWithMD5AndDES
代碼封裝工具類
public class JasyptUtil {
private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";
private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
/**
*
* @param text 待加密原文
* @param crack 鹽值(密鑰)
* @return 加密后的字符串
* @Description: Jasypt加密(PBEWithMD5AndDES)
*/
public static String encryptWithMD5(String text, String crack) {
//1.創(chuàng)建加解密工具實例
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//2.加解密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(PBEWITHMD5ANDDES);
config.setPassword(crack);
encryptor.setConfig(config);
//3.加密
return encryptor.encrypt(text);
}
/**
*
* @param text 待解密原文
* @param crack 鹽值(密鑰)
* @return 解密后的字符串
* @Description: Jasypt解密(PBEWithMD5AndDES)
*/
public static String decryptWithMD5(String text, String crack) {
//1.創(chuàng)建加解密工具實例
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//2.加解密配置
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm(PBEWITHMD5ANDDES);
config.setPassword(crack);
encryptor.setConfig(config);
//解密
return encryptor.decrypt(text);
}
/**
*
* @param text 待加密的原文
* @param crack 鹽值(密鑰)
* @return 加密后的字符串
* @Description: jasypt 加密(PBEWITHHMACSHA512ANDAES_256)
*/
public static String encryptWithSHA512(String text, String crack) {
//1.創(chuàng)建加解密工具實例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
//2.加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(crack);
config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 為減少配置文件的書寫,以下都是 Jasypt 3.x 版本,配置文件默認(rèn)配置
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
//3.加密
return encryptor.encrypt(text);
}
/**
*
* @param text 待解密原文
* @param crack 鹽值(密鑰)
* @return 解密后的字符串
* @Description: jasypt 解密(PBEWITHHMACSHA512ANDAES_256)
*/
public static String decryptWithSHA512(String text, String crack) {
//1.創(chuàng)建加解密工具實例
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
//2.加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(crack);
config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 為減少配置文件的書寫,以下都是 Jasypt 3.x 版本,配置文件默認(rèn)配置
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
//3.解密
return encryptor.decrypt(text);
}
}到此這篇關(guān)于springboot使用jasypt對配置文件加密,加密數(shù)據(jù)庫連接的文章就介紹到這了,更多相關(guān)springboot使用jasypt內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot中手動提交事務(wù)的實現(xiàn)方法
手動提交事務(wù)可以提供更靈活的控制,以便在分布式環(huán)境中處理事務(wù)的提交和回滾,本文就來介紹一下springboot中手動提交事務(wù)的實現(xiàn)方法,感興趣的可以了解一下2024-01-01
java8中定時任務(wù)最佳實現(xiàn)方式(實現(xiàn)原理)
這篇文章主要介紹了java8中定時任務(wù)最佳實現(xiàn)方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-12-12
springMVC配置環(huán)境實現(xiàn)文件上傳和下載
這篇文章主要為大家詳細(xì)介紹了springMVC配置環(huán)境實現(xiàn)文件上傳和下載的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
Log4j詳細(xì)使用教程_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Log4j的使用教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
這篇文章主要介紹了Java中的?HTTP?協(xié)議原理詳解,HTTP超文本傳輸協(xié)議,下文簡稱?HTTP,它的作用是用于實現(xiàn)服務(wù)器端和客戶端的數(shù)據(jù)傳輸?shù)?/div> 2022-07-07
微服務(wù)如何通過feign.RequestInterceptor傳遞參數(shù)
這篇文章主要介紹了微服務(wù)如何通過feign.RequestInterceptor傳遞參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java入門指南之IDEA高效調(diào)試與數(shù)組
調(diào)試是指在程序運行過程中,通過各種手段觀察程序的執(zhí)行狀態(tài)、變量值的變化等,以找出程序中存在的錯誤或異常的過程,這篇文章主要介紹了Java入門指南之IDEA高效調(diào)試與數(shù)組的相關(guān)資料,需要的朋友可以參考下2026-05-05
Java線程編程中Thread類的基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了Java線程編程中Thread類的基礎(chǔ)學(xué)習(xí)教程,Thread類包含諸多操作線程的方法,非常重要,需要的朋友可以參考下2015-12-12最新評論

