SpringBoot使用jasypt加解密密碼的實現(xiàn)方法
jasypt是一個通用的加解密庫,我們可以使用它在配置文件中對數(shù)據(jù)庫密碼進(jìn)行加密,以確保其安全性。
1、注入依賴
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version></dependency>
2、配置文件
#以數(shù)據(jù)庫密碼加密為例## 數(shù)據(jù)源配置spring.datasource.url=jdbc:mysql://lochost:3306/jasypt?characterEncoding=utf8spring.datasource.username=root#Fddt+VfcW5+j5lAbuOXxPB3mGb0iBLLe 是采用jasypt進(jìn)行加密以后生成的密文spring.datasource.password=ENC(Fddt+VfcW5+j5lAbuOXxPB3mGb0iBLLe)spring.datasource.driver-class-name=com.mysql.jdbc.Driver#jasypt加密的密匙jasypt.encryptor.password=abcderf(這個是自己設(shè)置的)
那么如何得到這個密文呢?
1、win+r cmd打開命令窗口 在你的maven庫中找到 jasypt-1.9.2.jar 包執(zhí)行下面的命令
java -cp D:Mavenrepositoryorgjasyptjasypt1.9.2jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input='密鑰(abcderf)' password=root(加密的密碼) algorithm=PBEWithMD5AndDES
然后復(fù)制密文即可
2、代碼生成(這種方法沒有使用過 參考鏈接:https://www.jb51.net/article/197600.htm)
import org.jasypt.util.text.BasicTextEncryptor;public class Test {public static void main(String[] args) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(鹽) textEncryptor.setPassword('PBEWithMD5AndDES'); //要加密的數(shù)據(jù)(數(shù)據(jù)庫的用戶名或密碼) String username = textEncryptor.encrypt('root'); String password = textEncryptor.encrypt('root'); System.out.println('username:'+username); System.out.println('password:'+password); }}
到此這篇關(guān)于SpringBoot使用jasypt加解密密碼的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringBoot加解密密碼內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章: