SpringBoot使用除了Jasypt對(duì)YML文件配置內(nèi)容進(jìn)行加密方式
1. 在pom.xml中添加Jasypt依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>>
2. 加密算法
// 1. 創(chuàng)建對(duì)象
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
// 秘鑰
config.setPassword("your-secret-key");
// 3. 加密算法,需要與解密算法一致
config.setAlgorithm("PBEWithMD5AndDES");
// 為減少配置文件的書寫,以下都是 Jasyp 3.x 版本,配置文件默認(rèn)配置
config.setKeyObtentionIterations( "1000");
config.setPoolSize("1");
config.setProviderName("JC");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
//需要加密的字符串
System.out.println( encryptor.encrypt("需加密的字符串"));
加密后的字符串

3. 在application.yml中配置加密相關(guān)信息
使用ENC(加密字符串)
url: jdbc:mysql://ip:port/test username: root password: ENC(w8bH76qYHSgDuxbwK0HsgPbmWZZMoxyVo0DvkOPSiY4=)
4. 配置的兩種方式
配置解密Bean并注入(硬編碼) 或 在Yml配置Jasypt(推薦)
4.1.1 配置Bean
@Configuration
@EnableEncryptableProperties
public class CustomEncryptConfig {
@Bean(name = "customStringEncryptor")
public static StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
// 2. 加解密配置
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("your-secret-key");
// 3. 解密算法,必須與加密算法一致
config.setAlgorithm("PBEWithMD5AndDES");
// 為減少配置文件的書寫,以下都是 Jasyp 3.x 版本,配置文件默認(rèn)配置
config.setKeyObtentionIterations( "1000");
config.setPoolSize("1");
config.setProviderName("JC");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
}
4.1.2 啟動(dòng)類中配置 jasypt.encryptor.bean
customStringEncryptor為上一步配置的Bean
@SpringBootApplication
public class SpringBootTestApplication
{
public static void main(String[] args)
{
System.setProperty("jasypt.encryptor.bean", "customStringEncryptor");
SpringApplication.run(SnSkApplication.class, args);
}
}
4.2.1 啟動(dòng)類中配置 jasypt.encryptor.bean
customStringEncryptor為上一步配置的Bean
jasypt:
encryptor:
algorithm: PBEWithMD5AndDES //PBEWITHHMACSHA512ANDAES_256更安全
iv-generator-classname: org.jasypt.iv.NoIvGenerator
password: your-secret-key
4.2.2 配置服務(wù)器運(yùn)行參數(shù)
java -Xms256M -Xmx256M -jar / java/app.jar --jasypt.encryptor.password=xxxxx
但是如果我們配置了password值,就需要在啟動(dòng)類加上@EnableEncryptableProperties這個(gè)注解,用于自動(dòng)解密。
5. 注意
代碼加密時(shí)的秘鑰和算法必須和配置的相同
1. 必須和在Bean中配置的秘鑰和算法 一致對(duì)應(yīng)
2. 必需在Yml中的jasypt.encryptor.password 和 jasypt.encryptor.algorithm 一致對(duì)應(yīng)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot整合Jasypt使用自定義注解+AOP實(shí)現(xiàn)敏感字段加解密
- SpringBoot整合Jasypt實(shí)現(xiàn)配置文件和數(shù)據(jù)庫(kù)字段敏感數(shù)據(jù)的加解密
- Springboot整合Jasypt對(duì)配置文件中的密碼加密的步驟
- SpringBoot集成Jasypt實(shí)現(xiàn)敏感信息加密保護(hù)功能
- SpringBoot整合jasypt實(shí)現(xiàn)數(shù)據(jù)加密的步驟
- SpringBoot整合jasypt實(shí)現(xiàn)重要數(shù)據(jù)加密
- SpringBoot利用jasypt實(shí)現(xiàn)配置文件加密的完整指南
相關(guān)文章
SpringBoot使用flyway初始化數(shù)據(jù)庫(kù)
這篇文章主要介紹了SpringBoot如何使用flyway初始化數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03
SpringCloud解決Feign異步回調(diào)問題(SpringBoot+Async+Future實(shí)現(xiàn))
這篇文章主要介紹了SpringCloud解決Feign異步回調(diào)問題(SpringBoot+Async+Future實(shí)現(xiàn)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Spring Boot 項(xiàng)目集成 Redisson 實(shí)現(xiàn)延遲隊(duì)列的詳細(xì)過程
本文介紹延遲隊(duì)列在訂單超時(shí)等場(chǎng)景的應(yīng)用及四種技術(shù)方案對(duì)比,推薦Redisson延遲隊(duì)列,提供項(xiàng)目結(jié)構(gòu)與測(cè)試源碼,對(duì)Spring Boot Redisson延遲隊(duì)列相關(guān)知識(shí)感興趣的朋友一起看看吧2025-06-06
idea 無法創(chuàng)建Scala class 選項(xiàng)的原因分析及解決辦法匯總
這篇文章主要介紹了idea 無法創(chuàng)建Scala class 選項(xiàng)的解決辦法匯總,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java查詢緩存之優(yōu)化重復(fù)查詢的執(zhí)行效率問題
這篇文章主要介紹了Java查詢緩存之優(yōu)化重復(fù)查詢的執(zhí)行效率問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
SpringBoot中的ThreadLocal保存請(qǐng)求用戶信息的實(shí)例demo
線程局部變量,創(chuàng)建一個(gè)線程變量后,針對(duì)這個(gè)變量可以讓每個(gè)線程擁有自己的變量副本,每個(gè)線程是訪問的自己的副本,與其他線程的相互獨(dú)立,本文介紹SpringBoot中的ThreadLocal保存請(qǐng)求用戶信息,需要的朋友可以參考下2024-05-05

