jasypt(靜安寺雲平台) 實現了 ApplicationContextInitializer 接口,重寫了獲取環境變量的方法,在容器初始化時對配置文件中的屬性進行判斷,若包類似"ENC(xxxx)"屬性值,則進行解密並返回。
1.引入maven依賴:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.指定加密屬性的密鑰(password),並對實際密碼進行加密(input):
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="this_is_password" password="hello_kitty" algorithm=PBEWithMD5AndDES
3.配置用於加密屬性的密鑰,以及使用生成好的密碼(通過ENC()包裹)
jasypt.encryptor.password=hello_kitty
mysql.spring.datasource.password=ENC(SqFjGqx/YqnnBJrefEj8dp6qz525ERKVGslRQ5bEQdU=)