SpringBoot整合jasypt加密配置文件敏感信息
SpringBoot整合jasypt加密配置文件敏感信息
在項目中我們需要對配置文件的一些敏感信息進(jìn)行加密處理,比如數(shù)據(jù)庫賬戶密碼,避免直接暴露出來,這種場景常常用于生產(chǎn)環(huán)境,我們不想讓開發(fā)人員知道生產(chǎ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);
}
}
- 啟動類
@SpringBootApplication
@MapperScan(basePackages = "com.llp.jasypt.mapper")
public class JasyptApplication {
public static void main(String[] args) {
SpringApplication.run(JasyptApplication.class, args);
}
}
- application.yml配置文件
這里我們可以對需要加密的字段進(jìn)行加密處理,比如url、username、password 或者說redis的一些賬戶信息等等。
jasypt:
encryptor:
# 加密的秘鑰
# password: llp
# 加密算法
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
property:
# 算法識別的前后綴,默認(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
#配置類型別名,通常指定實體類所在包,這樣我們在xml中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
#啟用自動駝峰配置
map-underscore-to-camel-case: true
#通過config-location可以指定mybatis-config.xml,這樣就可以用傳統(tǒng)的方式來配置mybatis
#config-location: classpath:mybatis-config.xml
- 啟動配置
通常我們不會將password寫在配置文件中,而是由運維人員進(jìn)行統(tǒng)一管理,這樣開發(fā)人員只能看到密文而不知道解密的秘鑰
jasypt:
encryptor:
# 加密的秘鑰
# password: llp
# 加密算法
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
property:
# 算法識別的前后綴,默認(rèn)ENC(),數(shù)據(jù)庫密文示例:password: "ENC(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)"
prefix: Enc(
suffix: )
如果打包后部署項目,可以使用如下命令在啟動項目時指定秘鑰:
#方式1: java -jar xxx.jar -Djasypt.encryptor.password=加密數(shù)據(jù)的秘鑰 #方式2: java -jar xxx.jar --jasypt.encryptor.password=加密數(shù)據(jù)的秘鑰
idea中如何配置啟動參數(shù)
-Djasypt.encryptor.password=llp

到此這篇關(guān)于SpringBoot整合jasypt加密配置文件敏感信息的文章就介紹到這了,更多相關(guān)SpringBoot jasypt加密敏感信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis?實現(xiàn)動態(tài)排序的多表查詢
本文將展示如何在 Java 項目中結(jié)合 MyBatis 實現(xiàn)動態(tài)排序,尤其是在涉及多表查詢的情況下,具有一定的參考價值,感興趣的可以了解一下2024-05-05
Spring如何通過注解引入外部資源(PropertySource?Value)
這篇文章主要為大家介紹了Spring通過注解@PropertySource和@Value引入外部資源的方法實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
springmvc HttpServletRequest 如何獲取c:forEach的值
這篇文章主要介紹了springmvc HttpServletRequest 如何獲取c:forEach的值方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Thymeleaf渲染網(wǎng)頁時中文亂碼的問題及解決
這篇文章主要介紹了Thymeleaf渲染網(wǎng)頁時中文亂碼的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

