在SpringBoot中通過jasypt進行加密解密的方法
1.用途
在SpringBoot中,通過jasypt可以進行加密解密. 這個是雙向的, 且可以配置密鑰.
2.使用:
2.1通過UT創(chuàng)建工具類,并認識jasypt
import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;
public class UtilTests {
@Test
public void jasyptTest() {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
// application.properties, jasypt.encryptor.password
encryptor.setPassword("abc");
// encrypt root
System.out.println(encryptor.encrypt("root"));
System.out.println(encryptor.encrypt("root"));
System.out.println(encryptor.encrypt("root"));
// decrypt, the result is root
System.out.println(encryptor.decrypt("UP/yojB7ie3apnh3mLTU7w=="));
System.out.println(encryptor.decrypt("ik9FE3GiYLiHwchiyHg9QQ=="));
System.out.println(encryptor.decrypt("9Obo/jq9EqmTE0QZaJFYrw=="));
}
}
可以看出, 每次生成的密碼是不一樣的, 但是通過密鑰,可以解密成一樣的明文.
2.2在SpringBoot中配置jasypt
2.2.1配置密鑰
jasypt.encryptor.password:abc
2.2.2使用
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/tmp?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.username: ENC(ik9FE3GiYLiHwchiyHg9QQ==)
spring.datasource.password: ENC(ik9FE3GiYLiHwchiyHg9QQ==)
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
2.2.3啟動時配置密鑰
java -jar -Djasypt.encryptor.password=abc xxx.jar
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
詳解Spring如何使用@Scheduled注解執(zhí)行定時任務(wù)
在現(xiàn)代的Java應(yīng)用程序中,定時任務(wù)是一種常見的需求,Spring框架提供了多種方式來管理定時任務(wù),其中??@Scheduled??注解因其簡潔和易用性而受到開發(fā)者的青睞,下面我們就來看看具體實現(xiàn)方法吧2025-08-08
Spring關(guān)閉Tomcat Servlet容器時內(nèi)存泄漏問題解決方案
這篇文章主要介紹了Spring關(guān)閉Tomcat Servlet容器時內(nèi)存泄漏問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10
Java中隊列Queue和Deque的區(qū)別與代碼實例
學過數(shù)據(jù)結(jié)構(gòu)的,一定對隊列不陌生,java也實現(xiàn)了隊列,下面這篇文章主要給大家介紹了關(guān)于Java中隊列Queue和Deque區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-08-08

