SpringBoot利用jasypt實(shí)現(xiàn)配置文件加密的完整指南
在實(shí)際開(kāi)發(fā)中,若出于安全考慮不想暴露一些敏感的配置,如數(shù)據(jù)庫(kù)密碼等,就需要對(duì)配置文件進(jìn)行加密,這個(gè)開(kāi)源工具就很好幫我們實(shí)現(xiàn)這個(gè)需求
思路就是把配置文件中的值手動(dòng)加密填上去,然后生產(chǎn)環(huán)境的秘鑰通過(guò)運(yùn)行參數(shù)傳遞,開(kāi)發(fā)測(cè)試環(huán)境可以直接把秘鑰寫(xiě)在配置文件
1.引入依賴
非Spring Boot的使用方式可查閱官方文檔
github.com/ulisesbocchio/jasypt-spring-boot
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>${jasypt.spring.boot.version}</version>
</dependency>
2.yml 加入配置
# 配置秘鑰
jasypt:
encryptor:
password: ${jasypt.encryptor.password:defaultPwd}
# 需要加密的字段(明文:123456),需要使用ENC() 包裹
db:
password: ENC(bVv/e16L6qPO+1pqEPMUYT5uW1EjsERTCoIliqABM+YMr6WURdxPy26La8wHCxgF)
3.測(cè)試
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = OrderApplication.class)
public class DemoApplicationTest {
@Autowired
ConfigurableEnvironment environment;
@Autowired
StringEncryptor stringEncryptor;
static {
//指定運(yùn)行環(huán)境密碼
System.setProperty("jasypt.encryptor.password", "password");
}
@Test
public void testEnvironmentProperties() {
//解密密文
System.out.println(environment.getProperty("db.password"));
}
@Test
public void encrypt() {
//此處得到加密后的文本,放入yml
System.out.println("密文: " + stringEncryptor.encrypt("123456"));
}
}
方法補(bǔ)充
SpringBoot配置文件加密
依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
設(shè)置秘鑰
jasypt.encryptor.password=秘鑰 開(kāi)發(fā)環(huán)境可以直接在配置文件中指定,生產(chǎn)環(huán)境可以使用啟動(dòng)命理 --jasypt.encryptor.password=XXX 進(jìn)行配置 ####加密解密
package com.itdfq.springboot;
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author duanfangqin 2022/7/11 10:56
* @implNote
*/
@SpringBootTest
public class JasyptTest {
@Autowired
private StringEncryptor stringEncryptor;
@Test
public void encrypt(){
System.out.println(stringEncryptor.encrypt("qweasd123"));
}
@Test
public void decrypt(){
System.out.println(stringEncryptor.decrypt("zI16ovOHuYiPIQaFx9cVlJF30bg5h3ql"));
}
}
配置文件密碼設(shè)置
#Redis服務(wù)器地址 spring.redis.host=119.3.234.108 #服務(wù)器密碼 spring.redis.password=ENC(nwElWn8r6aqVHDdAStIRiEXPAiE57qUF)
ENC(加密之后的密碼)
到此這篇關(guān)于SpringBoot利用jasypt實(shí)現(xiàn)配置文件加密的完整指南的文章就介紹到這了,更多相關(guān)SpringBoot jasypt配置文件加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在SpringBoot中更改默認(rèn)端口的方法總結(jié)
在本文中,小編將帶大家學(xué)習(xí)如何在 Spring Boot 中更改默認(rèn)端口,默認(rèn)情況下,嵌入式 Web 服務(wù)器使用 8080端口來(lái)啟動(dòng) Spring 引導(dǎo)應(yīng)用程序,有幾種方法可以更改該端口,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
通過(guò)Java代碼來(lái)創(chuàng)建view的方法
本文給大家分享通過(guò)java代碼創(chuàng)建view的方法,以TextView為例創(chuàng)建控件的方法,需要的的朋友參考下吧2017-08-08
Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問(wèn)題及解決方法(推薦)
下面小編就為大家分享一篇Java 關(guān)于eclipse導(dǎo)入項(xiàng)目發(fā)生的問(wèn)題及解決方法(推薦),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
JavaWeb應(yīng)用實(shí)例:用servlet實(shí)現(xiàn)oracle 基本增刪改查
本篇文章主要介紹了JavaWeb應(yīng)用實(shí)例:用servlet實(shí)現(xiàn)oracle 基本增刪改查,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01
Java8中利用stream對(duì)map集合進(jìn)行過(guò)濾的方法
這篇文章主要給大家介紹了關(guān)于Java8中利用stream對(duì)map集合進(jìn)行過(guò)濾的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

