關(guān)于Springboot數(shù)據(jù)庫配置文件明文密碼加密解密的問題
有時(shí)候因?yàn)榘踩珕栴},需要把配置文件的中數(shù)據(jù)庫用戶名密碼由明文改成密文,大多數(shù)其實(shí)是為了應(yīng)付甲方而已。
1.pom.xml引入依賴
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
2.自己想一個(gè)秘鑰,然后弄一個(gè)main方法來測(cè)試和生成加密串,下面例子把“password”當(dāng)做秘鑰,加密 xiaoming 字符串。同樣可以把加密的打印出來,放到解密里面去驗(yàn)證一下
//給配置文件加密
public static void main(String[] args) {
// 加密
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//自己設(shè)置的秘鑰
textEncryptor.setPassword("password");
String userName = textEncryptor.encrypt("xiaoming");
System.out.println(userName);
// 解密
BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor();
textEncryptor2.setPassword("password");
String oldPassword = textEncryptor2.decrypt("avU0Q/XfNMXcgOgowdcfLfB1FDdApc292pzeq8/uvrllChedBJvj4A==");
System.out.println(oldPassword);
System.out.println("--------------------------");
}3.springboot配置文件 application.properties中添加配置
jasypt.encryptor.password=password spring.datasource.driver-class-name=oracle.jdbc.OracleDriver spring.datasource.url=jdbc:oracle:thin:@192.168.100.123:7029:base spring.datasource.username=ENC(c31B0jWJp3EGFwqSkrUzhY//4CY/sO) spring.datasource.password=ENC(+KUeW5dB03CxJYz9oVV2flbYW5xs1+)
要先聲明秘鑰,然后把剛main方法中加密出來的字符串替換原來的,注意一定要用ENC()把字符串包住才行。
然后重啟就完事,就是這么簡單。
到此這篇關(guān)于Springboot數(shù)據(jù)庫配置文件明文密碼加密解密的文章就介紹到這了,更多相關(guān)Springboot數(shù)據(jù)庫密碼加密解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring Boot 配置多個(gè)RabbitMQ
本篇文章主要介紹了Spring Boot 配置多個(gè)RabbitMQ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Java硬幣翻轉(zhuǎn)倍數(shù)遞增試算實(shí)例
這篇文章主要介紹了Java硬幣翻轉(zhuǎn)倍數(shù)遞增試算實(shí)例,有需要的朋友可以參考一下2013-12-12
IDEA新建javaWeb以及Servlet簡單實(shí)現(xiàn)小結(jié)
這篇文章主要介紹了IDEA新建javaWeb以及Servlet簡單實(shí)現(xiàn)小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11

