App下載

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

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

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

1.導(dǎo)入依賴(lài)

	<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所在位置,打開(kāi)cmd

找到j(luò)asypt-1.9.2.jar所在位置,打開(kāi)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.在啟動(dòng)類(lèi)Application.java中配置注解,配置完成

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

6.小結(jié)

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

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

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

7.問(wèn)題

問(wèn)題描述:在Spring Boot中使用jasypt-spring-boot進(jìn)行加密,但是提示:

 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更改了默認(rèn)的加密算法,最后的辦法是把版本降到2.1.2

總結(jié)

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


0 人點(diǎn)贊