點擊上方藍色“大數據實戰演練”,選擇“設爲星標”或“置頂”
回覆“資源”領取獨家整理的學習資料!
每一個成功人士的背後,必定曾經做出過勇敢而又孤獨的決定。
放棄不難,但堅持很酷~
先看一份典型的配置文件
.. 省略 ...
## 配置MySQL數據庫連接
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.xx.xxx:3306/test?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8
username: root
password: 123456
... 省略 ...
這是節選自某個典型的 Spring Boot 項目的 application.yml 配置文件。
噓... 偷偷告訴我,是不是很多小夥伴也都是這麼寫的?
這乍一看沒啥問題,很多人會覺得理所當然。包括我自己也看到過很多的項目(包括很多開源項目)是這麼寫的。
但仔細一琢磨,發現裏面有明文密碼呀,這是不是就很危險?!尤其你的項目要是上傳到 github 公共庫的話,是可以被任何人查閱的,如果這樣導致自己或公司的數據庫泄露,後果可想而知。。。
總而言之,在配置文件中的所有密碼都應該做加密處理。今天我們就來講一下加密組件 Jasypt 這個強大的庫。
引入依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
配置加密相關參數
方式一:使用 properties 文件配置
jasypt.encryptor.algorithm=PBEWithMD5AndDES # 默認的加密方式,還可以是PBEWithMD5AndTripleDES
jasypt.encryptor.password=jasypt # 加解密所需的salt(鹽)
jasypt.encryptor.property.prefix=ENC( # 標記密文的前綴
jasypt.encryptor.property.suffix=) # 標記密文的後綴
方式二:使用 yml 文件配置
jasypt:
encryptor:
password: jasypt
algorithm: PBEWithMD5AndDES
property:
prefix: ENC(
suffix: )
方式三:使用啓動參數配置(爲了防止salt(鹽)泄露,反解出密碼。可以在項目部署的時候作爲參數傳入salt(鹽)值,推薦使用)
1)idea 配置方法
2)啓動 jar 包命令:
java -Djasypt.encryptor.password=jasypt -jar xxx.jar
上面的 jasypt.encryptor.password 配置是指定 jasypt 加解密明文的密鑰。即:jasypt 會根據該值加密你的明文,然後你將密文配置在配置文件中顯示;程序啓動的時候,jasypt 會將你的密文根據密鑰解密,進行驗證。
兩種生成密文的方式
方式一:使用 spring boot 單元測試
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.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptTest {
@Autowired
private StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
//加密123456
String result = stringEncryptor.encrypt("123456");
System.out.println(result);
}
}
在單元測試中,spring boot 會讀取 application 配置文件中的 jasypt.encryptor.password ,對明文 123456 進行加密。
方式二:使用工具類
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/**
* Jasypt加密工具類
*/
public class JasyptUtil {
/**
* Jasypt生成加密結果
* @param password 配置文件中設定的加密密
* @param value 加密值
* @return
*/
public static String encyptPwd(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
String result = encryptor.encrypt(value);
return result;
}
/**
* 解密
* @param password 配置文件中設定的加密密碼
* @param value 解密密文
* @return
*/
public static String decyptPwd(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
String result = encryptor.decrypt(value);
return result;
}
public static SimpleStringPBEConfig cryptor(String password){
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
return config;
}
public static void main(String[] args){
//加密
System.out.println(encyptPwd("jasypt","123456"));
//解密
System.out.println(decyptPwd("jasypt","lnzpDZItgjAntHqsYPFTew=="));
}
}
配置密文
將生成的加密密文配置在配置文件中即可,如示例所示,密文已用 test() 所標記,在啓動時會解析所有 jasypt.encryptor.property.prefix/suffix 標記的密文。
示例:
jasypt:
encryptor:
password: jasypt
algorithm: PBEWithMD5AndDES
property:
prefix: test(
suffix: )spring:
datasource:
password: test(lnzpDZItgjAntHqsYPFTew==)
噓... 快去加密
好了,說了這麼多,如果你項目的配置文件中的重要信息沒有加密的話,答應我,二話別說,趕快全部偷偷去改掉,快!速度!跑步前進!
往期推薦
掃一掃,我們的故事就開始了。
如果這篇文章對你有所啓發,點贊、轉發都是一種支持!
另外公衆號改變了推送規則,大家看文章不要忘記點擊最下方的在看,點贊按鈕,這樣微信自動識別爲常看公衆號,否則很可能推送的文章可能淹沒在別的文章找不到,謝謝大家
讓我知道你在看
本文分享自微信公衆號 - 大數據實戰演練(gh_f942bfc92d26)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。