1、需求
在使用SpringBoot開發(fā)過程中,會將一些敏感信息配置到SpringBoot項目的配置文件中(不考慮使用配置中心的情況 ),例如數(shù)據(jù)庫的用戶名和密碼、Redis的密碼等。為了保證敏感信息的安全,我們需要將此類數(shù)據(jù)進行加密配置。
2、操作步驟
2.1 添加依賴
目前通用的做法是使用 jasypt 對數(shù)據(jù)庫用戶名或者密碼進行加密,在springboot項目的POM中添加如下依賴,目前最新的版本是3.0.3,但是我們不用最新版本,而是使用2.1.2版本。后邊會說明原因。
<!--數(shù)據(jù)庫密碼加密依賴-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2.2 生成密文
依賴添加之后,會在你本地的maven倉庫中下載相關(guān)的依賴包,進入到你自己的maven倉庫,通過路徑/org/jasypt/jasypt/1.9.3,找到 jasypt-1.9.3包。
- Windows系統(tǒng) 直接在文件地址欄輸入cmd進入到命令行。
- Mac系統(tǒng)通過Terminal終端進入到對應(yīng)路徑即可。
如下命令:
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=helloWorld algorithm=PBEWithMD5AndDES
- input:輸入內(nèi)容,此處指的是數(shù)據(jù)庫密碼。
- password:不是指的密碼,而是加密所使用的“鹽”,可以隨便定義。
- algorithm:加密所使用的算法,非必填,此處使用“PBEWithMD5AndDES” 算法。
如果想了解其他配置可以查看如下配置類:
com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties
執(zhí)行命令顯示如下信息:
>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=helloWorld algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.381-b09
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: root
password: helloWorld
----OUTPUT----------------------
1vAgkftBPnIYh/gjCokbFA==
OUTPUT即為加密后輸入的內(nèi)容。
同樣的操作,將“input”改為數(shù)據(jù)庫密碼如“123”再執(zhí)行一次操作。
>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123" password=helloWorld algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.381-b09
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: 123
password: helloWorld
----OUTPUT----------------------
LPbn37xuIIAfCkaermp5cQ==
OUTPUT即為密碼加密后的數(shù)據(jù)。
2.3 修改Springboot 數(shù)據(jù)庫配置
生成加密密文后,修改項目數(shù)據(jù)庫連接的用戶名和密碼:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true
username: ENC(1vAgkftBPnIYh/gjCokbFA==)
password: ENC(LPbn37xuIIAfCkaermp5cQ==)
此時,已經(jīng)配置完成。
2.4 SpringBoot項目配置文件中配置“加密鹽”
為了能夠使程序獲取我們加密前的數(shù)據(jù),需要在項目配置文件中配置“加密鹽”,即2.2節(jié)中所說的“password”,如下所示:
jasypt:
encryptor:
password: helloWorld
但是在配置文件中配置加密鹽也是不安全的,如果別人知道了加密后的密文,又知道了加密鹽,就可以通過如下解密命令進行解密,這樣就會導致我們的密碼泄露。
>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="LPbn37xuIIAfCkaermp5cQ==" password=helloWorld algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.381-b09
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: jeo6NNy5rK0x3rDkywsvBw==
password: esunny_Qwer2023
----OUTPUT----------------------
123
在本地開發(fā)時,可以這樣操作,或者通過配置idea的虛擬機參數(shù)也是可以的
上線操作時通過增加啟動參數(shù)進行配置,以保證安全。
3、問題解答
在2.1節(jié)添加依賴中我們使用的 2.1.2版本,而沒有使用最新的3.0.3版本。本人親測,在使用3.0.3版本時,按照以上配置完成后啟動項目,會出新如下報錯信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'spring.datasource.dynamic.datasource.master.password' to java.lang.String:
Reason: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.dynamic.datasource.master.password' to java.lang.String
Action:
Update your application's configuration
Disconnected from the target VM, address: '127.0.0.1:56043', transport: 'socket'
較新的版本更改了相關(guān)算法,會出現(xiàn)如上錯誤,降低版本即可。文章來源:http://www.zghlxwxcb.cn/news/detail-688265.html
注意: 如果使用了2.1.2版本,但是沒有配置 加密鹽,也會報上邊的錯誤,按照如上配置加密鹽即可。文章來源地址http://www.zghlxwxcb.cn/news/detail-688265.html
到了這里,關(guān)于SpringBoot項目配置文件數(shù)據(jù)庫用戶名密碼加密的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!