功能介紹
大家都知道在spring boot開發過程中,需要在配置文件里配置許多信息,如數據庫的連接信息等,如果不加密,傳明文,數據庫就直接暴露了,相當于"裸奔"了,因此需要進行加密處理才行。
在項目中使用jasypt-1.9.4.jar包,能夠實現對明文進行加密,對密文進行解密。配置相關加密信息,就能夠實現在項目運行的時候,自動把配置文件中已經加密的信息解密成明文,供程序使用
下面話不多說了,來一起看看詳細的介紹吧
使用說明
1.pom引入依賴
1
2
3
4
5
|
<dependency> <groupid>com.github.ulisesbocchio</groupid> <artifactid>jasypt-spring-boot-starter</artifactid> <version> 2.1 . 1 </version> </dependency> |
2.配置文件application.yaml
1
2
3
4
5
6
7
|
******************加解密相關配置******************* jasypt: encrytor: #用來加解密的salt值 password: 123456 #用來使用新的算法,默認為org.jasypt.salt.noopivgenerator,這樣的話我們就無法使用命令行中生成的密文 ivgeneratorclassname: org.jasypt.salt.randomivgenerator |
參數解釋:
- password:加密時候要使用salt值
- 對于ivgeneratorclassname,jara包中封裝類默認為org.jasypt.salt.noopivgenerator,這個時候我們如果使用junit生成密文,那么只會生成24位密鑰,與命令行中用命令生成的不一樣,后面會詳細講解。
3.代碼解析
首先我們需要知道的事加解密的方法,只有知道了如何加密才能夠在配置文件中設置相關參數的密文,這里涉及到兩種方式的加密:
a.命令行加密
如果我們項目上線了,需要修改配置文件中的信息,這個時候我們可能要通過命令行的方式去加密(前提:保證你的salt值和你的項目中定義的一致)
i.找到maven倉庫本地地址,如:c:\users\kfzx-xuming\.m2\repository 在這里面找到jasypt-1.9.4.jar所在位置
ii.進入文件夾,運行cmd命令
加密:java -cp jasypt-1.9.4.jar org.jasypt.intf.cli.jasyptpbestringencryptioncliinput=pass1234password=12345algorithm=pbewithmd5anddes
參數說明:
- input:加上需要加密的明文
- password:加上salt值(需要和項目中的application.yaml的password 一致)
- algorithm:加上加密算法(默認使用的就是pbewithmd5anddes)
這個時候我們可以看到下面的加密結果:
下面的output中就是我們對明文pass1234使用salt值為12345加密的結果
解密:java -cp jasypt-1.9.4.jar org.jasypt.intf.cli.jasyptpbestringdecryptioncli input=pdfvckrynvoktpej+081g70kzvwv2alrtok2ejrjkksnmbu4c4ix+q== password=12345 algorithm=pbewithmd5anddes
這個時候我們可以看到解密結果:
b.在eclipse中用junit運行代碼對明文加密解密
前提已經在配置文件中配置了jasypt相關信息
jasypt提供了封裝類stringencryptor,可以通過代碼來加解密,我們可以使用這個類運行相關方法
junit相關代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import org.jasypt.encryption.stringencryptor; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.context.embedded.embeddedservletcontainercustomizer; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springjunit4classrunner; import static sun.plugin.javascript.navig.jstype.embed; @runwith (springjunit4classrunner. class ) @springboottest public class jasypttest { @autowired stringencryptor encryptor; //加密 @test public void getpass(){ string name = encryptor.encrypt( "hello" ); system.out.println( "加密結果:" +name); //解密 @test public void passdecrypt(){ string username = encryptor.decrypt( "7ubc9fvlpl05ipepzgsdt6qcjuq9hvdyc0vuigp4hy=" ); system.out.println( "解密結果:" +username); } } |
運行結果如下:
這個時候我們就得到了想要的密文,直接粘貼到配置文件中即可
使用方法如下:
i.在配置文件application.yaml相關位置把明文替換成密文,用enc()包裹:
1
2
3
4
|
************** 加解密相關測試配置信息*************** test: code: username: enc(pdfvckrynvoktpej+081g70kzvwv2alrtok2ejrjkksnmbu4c4ix+q==) |
ii.在相應的位置直接讀取使用即可,下面我們寫一個controller類測試一下運行解密的效果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import org.springframework.beans.factory.annotation.value; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @controller public class jasyptcontroller { @value ( "${cmd.username}" ) private string cmdusername; @requestmapping ( "/hello" ) public string testjasypt() { return cmdusername; } } |
這個時候我們啟動項目就能夠看到我們再配置中設置的密文對應的明文
至此配置文件的加解密的使用方法就介紹完了
4.補充說明
對于上述配置文件中的ivgeneratorclassname再進行一個詳細的介紹
對于上述的junit中使用的stringencryptor封裝類,他是可以通過讀取配置文件中的信息進行加解密相關參數進行初始化,通過閱讀遠嗎,我們可以發現,初始化config的時候會跳轉到如下的地方進行設置:
如果在配置參數中沒有設置ivgeneratorclassname,那么默認就是org.jasypt.salt.noopivgenerator,那么在運行加解密的時候就會生成一個24位的密文,如圖:
但是我們可以看到上面用命令行生成的卻比這個廠,這個時候如果我們把命令行中生成的密文粘貼到配置文件中則springboot就會啟動不了,junit也會報錯解析,把這個密文用命令解析發現也會報錯
這個說明命令行中的加解密不是通過stringencryptor類來操作的,那是走那邊的呢?
通過查閱資料我們發現了下面的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package cn.linjk.ehome; import org.jasypt.encryption.pbe.standardpbestringencryptor; import org.jasypt.encryption.pbe.config.environmentpbeconfig; import org.junit.test; public class jasypttest { @test public void testencrypt() throws exception { standardpbestringencryptor standardpbestringencryptor = new standardpbestringencryptor(); environmentpbeconfig config = new environmentpbeconfig(); config.setalgorithm( "pbewithmd5anddes" ); // 加密的算法,這個算法是默認的 config.setpassword( "12345" ); // 加密的密鑰 standardpbestringencryptor.setconfig(config); string plaintext = "hello" ; string encryptedtext = standardpbestringencryptor.encrypt(plaintext); system.out.println(encryptedtext); } } |
這個時候我們運行一下,得到下面的結果:
把這個密文用命令進行解密發現也是成功的,查看源碼(下圖):可以看出,命令行如果沒有設置ivgeneratorclassname那么默認就會new randomivgenerator,就是這一步導致了生成了不一樣的密文:
綜上所述,結合場景,如果我們是項目需要上線了,不方便運行junit去生成密文,填入配置文件,需要用命令行對明文加密,那一定要在配置文件中設置ivgeneratorclassname值!!!
公司電腦沒有辦法用外網上,圖片用手機拍的,如果感覺模糊,請見諒,大家相互學習~~~~~~
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/charles8866/p/10478796.html