關于Springboot數(shù)據(jù)庫配置文件明文密碼加密解密的問題
有時候因為安全問題,需要把配置文件的中數(shù)據(jù)庫用戶名密碼由明文改成密文,大多數(shù)其實是為了應付甲方而已。
1.pom.xml引入依賴
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
2.自己想一個秘鑰,然后弄一個main方法來測試和生成加密串,下面例子把“password”當做秘鑰,加密 xiaoming 字符串。同樣可以把加密的打印出來,放到解密里面去驗證一下
//給配置文件加密 public static void main(String[] args) { // 加密 BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //自己設置的秘鑰 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()把字符串包住才行。
然后重啟就完事,就是這么簡單。
到此這篇關于Springboot數(shù)據(jù)庫配置文件明文密碼加密解密的文章就介紹到這了,更多相關Springboot數(shù)據(jù)庫密碼加密解密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA新建javaWeb以及Servlet簡單實現(xiàn)小結
這篇文章主要介紹了IDEA新建javaWeb以及Servlet簡單實現(xiàn)小結,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11