springboot配置文件中敏感數(shù)據(jù)(賬號密碼)加密方式
背景
原本項目中數(shù)據(jù)庫和中間件賬號密碼都是直接用明文配置的,畢竟這樣最方便開發(fā),平時也沒人在乎這個。
但是部署到實際項目中,甲方進行安全檢查第一個就查到這個,要求我們整改,那只能整了。
網(wǎng)上找了一些資料,好像大都用Jasypt加密工具操作,Jasypt官網(wǎng)看其他博客看了下,發(fā)現(xiàn)使用起來比較方便,直接簡單記錄下,
擴展:
直接預研敏感數(shù)據(jù)加密技術(shù),不只是對配置文件加密,對其他接口經(jīng)過后臺的敏感數(shù)據(jù)也進行了解,包括數(shù)據(jù)加密、加密后的技術(shù)怎么進行模糊查詢等等,會在后續(xù)博客中進行編寫。
當然這都是后話,下面直接操作Jasypt進行配置文件賬號密碼加密。
實踐Jasypt
1、引入依賴
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency>
如果項目中啟動類沒有 @SpringBootApplication或@EnableAutoConfiguration注解,
那么需要引入jasypt-spring-boot依賴且需要創(chuàng)建配置類
并啟用 @Configuration和@EnableEncryptableProperties,進行聲明。
2、賬號密碼轉(zhuǎn)成加密值
創(chuàng)建一個工具類把我們的賬號密碼進行加密
import org.jasypt.properties.PropertyValueEncryptionUtils; import org.jasypt.util.text.BasicTextEncryptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Jasypt加密工具類 * @author ppp * @date 2023/1/5 */ public class JasyptUtil { private static final Logger logger = LoggerFactory.getLogger(JasyptUtil.class); /** * 加密使用密鑰,需要在和配置文件中的jasypt.encryptor.password保持一致 */ private static final String PRIVATE_KEY = "demo"; /** * BasicTextEncryptor對象初始化使用的算法就是"PBEWithMD5AndDES" * 點擊進源碼構(gòu)造方法中就可以看到下面的設(shè)置 * this.encryptor.setAlgorithm("PBEWithMD5AndDES"); */ private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); static { basicTextEncryptor.setPassword(PRIVATE_KEY); } /** * 明文加密 * * @param plaintext 明文 * @return String */ public static String encrypt(String plaintext) { logger.info("明文字符串為:{}", plaintext); String ciphertext = basicTextEncryptor.encrypt(plaintext); logger.info("密文字符串為:{}", ciphertext); return ciphertext; } /** * 解密 * * @param ciphertext 密文 * @return String */ public static String decrypt(String ciphertext) { logger.info("密文字符串為:{}", ciphertext); ciphertext = "ENC(" + ciphertext + ")"; if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)) { String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext, basicTextEncryptor); logger.info("明文字符串為:{}", plaintext); return plaintext; } logger.error("解密失??!"); return ""; } public static void main(String[] args) { String encrypt = encrypt("123456"); String test = decrypt(encrypt); } }
3、配置文件添加配置
application.yml中添加配置
jasypt: encryptor: # 指定加密密鑰,生產(chǎn)環(huán)境請放到啟動參數(shù)里面 -Djasypt.encryptor.password=加密密鑰 password: demo # 指定解密算法,需要和加密時使用的算法一致 algorithm: PBEWithMD5AndDES # 指定initialization vector類型 iv-generator-classname: org.jasypt.iv.NoIvGenerator
4、替換賬號密碼
把application.yml中需要加密的賬號密碼都換成 ENC(密文) 格式即可
啟用項目后他會檢測配置文件中ENC樣式的數(shù)據(jù)把里面的密文解密出來給框架使用。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何在maven本地倉庫中添加oracle的jdbc驅(qū)動
文章介紹了在Maven項目中添加Oracle數(shù)據(jù)庫驅(qū)動ojdbc5時遇到的問題以及解決問題的兩種方法,方法一為簡單粗暴,但沒有體現(xiàn)Maven倉庫的作用,需要手動管理jar包,方法二為在Maven本地倉庫中添加Oracle的JDBC驅(qū)動,過程較為繁瑣,但配置一次后可以多次使用2024-11-11Spring Security基于過濾器實現(xiàn)圖形驗證碼功能
驗證碼就是為了防止惡意用戶采用暴力重試的攻擊手段而設(shè)置的一種防護措施,接下來在Spring Security的環(huán)境中,我們可以用兩種方案實現(xiàn)圖形驗證碼,具體實現(xiàn)方法跟隨小編一起看看吧2021-09-09