SpringBoot整合jasypt加密配置文件敏感信息
SpringBoot整合jasypt加密配置文件敏感信息
在項(xiàng)目中我們需要對(duì)配置文件的一些敏感信息進(jìn)行加密處理,比如數(shù)據(jù)庫賬戶密碼,避免直接暴露出來,這種場(chǎng)景常常用于生產(chǎn)環(huán)境,我們不想讓開發(fā)人員知道生產(chǎn)庫的密碼,有運(yùn)維人員統(tǒng)一管理。
- 引入依賴
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
- 加密工具類
public class JasyptUtils { public static void main(String[] args) { //這里假設(shè)我們的數(shù)據(jù)庫密碼是root String info = encrypt("root"); //加密 TCFVL/wsN9AxelTDQyP/3g== System.out.println(info); //解密 root System.out.println(decrypt(info)); } /** * 加密 * * @param plaintext 明文 * @return */ public static String encrypt(String plaintext) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); // 指定算法 config.setAlgorithm("PBEWithMD5AndDES"); // 指定秘鑰,和yml配置文件中保持一致 config.setPassword("llp"); encryptor.setConfig(config); // 生成加密數(shù)據(jù) return encryptor.encrypt(plaintext); } /** * 解密 * * @param data 加密后數(shù)據(jù) * @return */ public static String decrypt(String data) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); config.setAlgorithm("PBEWithMD5AndDES"); config.setPassword("llp"); encryptor.setConfig(config); // 解密數(shù)據(jù) return encryptor.decrypt(data); } }
- 啟動(dòng)類
@SpringBootApplication @MapperScan(basePackages = "com.llp.jasypt.mapper") public class JasyptApplication { public static void main(String[] args) { SpringApplication.run(JasyptApplication.class, args); } }
- application.yml配置文件
這里我們可以對(duì)需要加密的字段進(jìn)行加密處理,比如url、username、password 或者說redis的一些賬戶信息等等。
jasypt: encryptor: # 加密的秘鑰 # password: llp # 加密算法 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator property: # 算法識(shí)別的前后綴,默認(rèn)ENC(),數(shù)據(jù)庫密文示例:password: "ENC(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)" prefix: Enc( suffix: ) spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/llp?autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true username: Enc(TCFVL/wsN9AxelTDQyP/3g==) password: Enc(TCFVL/wsN9AxelTDQyP/3g==) #mybatis配置 mybatis: #指定要掃描的mapper.xml位置; classpath:mapper/*.xml 掃描在類路徑下的mapper目錄下的。xml文件 mapper-locations: classpath:mapper/*.xml #配置類型別名,通常指定實(shí)體類所在包,這樣我們?cè)趚ml中resultType="com.llp.springboot.mybatis.bean.Monster"就可以簡寫成Monster type-aliases-package: com.llp.springboot.jasypt.entity #配置mybatis sql打印日志 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #啟用自動(dòng)駝峰配置 map-underscore-to-camel-case: true #通過config-location可以指定mybatis-config.xml,這樣就可以用傳統(tǒng)的方式來配置mybatis #config-location: classpath:mybatis-config.xml
- 啟動(dòng)配置
通常我們不會(huì)將password寫在配置文件中,而是由運(yùn)維人員進(jìn)行統(tǒng)一管理,這樣開發(fā)人員只能看到密文而不知道解密的秘鑰
jasypt: encryptor: # 加密的秘鑰 # password: llp # 加密算法 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator property: # 算法識(shí)別的前后綴,默認(rèn)ENC(),數(shù)據(jù)庫密文示例:password: "ENC(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)" prefix: Enc( suffix: )
如果打包后部署項(xiàng)目,可以使用如下命令在啟動(dòng)項(xiàng)目時(shí)指定秘鑰:
#方式1: java -jar xxx.jar -Djasypt.encryptor.password=加密數(shù)據(jù)的秘鑰 #方式2: java -jar xxx.jar --jasypt.encryptor.password=加密數(shù)據(jù)的秘鑰
idea中如何配置啟動(dòng)參數(shù)
-Djasypt.encryptor.password=llp
到此這篇關(guān)于SpringBoot整合jasypt加密配置文件敏感信息的文章就介紹到這了,更多相關(guān)SpringBoot jasypt加密敏感信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中Formatter和Converter用法和區(qū)別小結(jié)
本文主要介紹了SpringBoot中Formatter和Converter用法和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring?Security方法級(jí)安全控制@PreAuthorize注解的靈活運(yùn)用小結(jié)
本文將帶著大家講解?@PreAuthorize?注解的核心原理、SpEL?表達(dá)式機(jī)制,并通過的示例代碼演示如何在實(shí)際項(xiàng)目中靈活運(yùn)用該注解實(shí)現(xiàn)細(xì)粒度的權(quán)限控制,感興趣的朋友一起看看吧2025-04-04Java中BigDecimal比較大小的3種方法(??compareTo()、??equals()??和??compar
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal比較大小的3種方法,方法分別是??compareTo()、??equals()??和??compareTo()??,在Java中使用BigDecimal類來進(jìn)行精確的數(shù)值計(jì)算,需要的朋友可以參考下2023-11-11Java中線程的等待與喚醒_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在Object.java中,定義了wait(), notify()和notifyAll()等接口。wait()的作用是讓當(dāng)前線程進(jìn)入等待狀態(tài),同時(shí),wait()也會(huì)讓當(dāng)前線程釋放它所持有的鎖。下面通過本文給大家介紹Java中線程的等待與喚醒知識(shí),感興趣的朋友一起看看吧2017-05-05idea配置連接數(shù)據(jù)庫的超詳細(xì)步驟
這篇文章主要介紹了idea配置連接數(shù)據(jù)庫的超詳細(xì)步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Mybatis plus的自動(dòng)填充與樂觀鎖的實(shí)例詳解(springboot)
這篇文章主要介紹了Mybatis plus的自動(dòng)填充與樂觀鎖的實(shí)例詳解(springboot),本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Struts2+Hibernate實(shí)現(xiàn)數(shù)據(jù)分頁的方法
這篇文章主要介紹了Struts2+Hibernate實(shí)現(xiàn)數(shù)據(jù)分頁的方法,結(jié)合實(shí)例形式分析了Struts2結(jié)合Hibernate實(shí)現(xiàn)數(shù)據(jù)分頁的原理,步驟與相關(guān)實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-03-03