App下載

SpringBoot配置文件中數(shù)據(jù)庫加密方法 詳細步驟

猿友 2021-07-16 11:25:38 瀏覽數(shù) (4731)
反饋

在開發(fā)程序系統(tǒng)的過程中,我們時常需要考慮系統(tǒng)安全的問題,因此在配置文件中是不能出現(xiàn)明文密碼。接下來,我將會為大家詳細地介紹一下Spring Boot配置文件數(shù)據(jù)庫密碼加密的方法。

1.導(dǎo)入依賴

	<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>

2.找到j(luò)asypt-1.9.2.jar所在位置,打開cmd

找到j(luò)asypt-1.9.2.jar所在位置,打開cmd

3.輸入如下命令執(zhí)行

java -cp  jasypt-1.9.2.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=sad124f1f1rf1fgt5 algorithm=PBEWithMD5AndDES

input :明文密碼

password:要加的鹽(可自己設(shè)置)

algorithm:加密算法,這里使用 PBEWithMD5AndDES

結(jié)果如下,OUTPUT就是加密之后的密文(密碼)

結(jié)果輸出

4.在application.yml文件中配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: ENC(HPPHfpHVXzNf00SQLBrjug==)
    password: ENC(F8ckXFGTF6Wu8dQ51hoNBw==)
    url: jdbc:mysql://localhost:3306/foodie-shop-dev?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
jasypt:
  encryptor:
    password: sad124f1f1rf1fgt5

5.在啟動類Application.java中配置注解,配置完成

//配置數(shù)據(jù)庫加密注解
@EnableEncryptableProperties

6.小結(jié)

除了加密MySQL密碼的例子之外,當(dāng)配置文件中有多個密碼、Token等需要加密時,這個方法就比較方便了。

除了支持字符串秘鑰,jasypt還支持key文件秘鑰,更加安全。

在安全要求很高時,還可以使用jasypt默認PBEWITHHMACSHA512ANDAES_256加密算法,該算法需要安裝JCE支持。

7.問題

問題描述:在Spring Boot中使用jasypt-spring-boot進行加密,但是提示:

 Description:
 Failed to bind properties under 'spring.datasource.password' to java.lang.String:
     Reason: Failed to bind properties under 'spring.datasource.password' to java.lang.String
 Action:
  Update your application's configuration

解決辦法:

3.0.2更改了默認的加密算法,最后的辦法是把版本降到2.1.2

總結(jié)

以上就是關(guān)于SpringBoot配置文件中數(shù)據(jù)庫加密方法的詳細步驟的全部內(nèi)容,想要了解更多相關(guān)springboot內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章!


0 人點贊