一、PBEWithMD5AndDES加密算法
二、springboot集成jasypt
pom
<!--配置文件加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
配置密鑰
配置文件添加
jasypt.encryptor.password: demo
獲取密文
//如果有多個配置文件,可用通過ActiveProfiles來指定配置文件;將匹配到配置文件:application-18011.properties
@ActiveProfiles("18011")
@SpringBootTest(classes = DemoApplication.class)
public class PigAdminApplicationTest {
//從spring環境中獲取bean,這個bean是從哪里來的呢?jasypt的starter
@Autowired
private StringEncryptor stringEncryptor;
@Test
public void testEnvironmentProperties() {
//將明文加密
System.out.println(stringEncryptor.encrypt("root"));
System.out.println(stringEncryptor.encrypt("123456"));
//解密
System.out.println(stringEncryptor.decrypt("bsK6LoI1XJrAFvynypY2Og=="));
}
}
在配置文件中使用
spring.datasource.username=ENC(PquhbjSL8UlUCK91LvuJNg==)
spring.datasource.password=ENC(8r2VooGyAOd+Q2+FpgHu8Q==)
springboot啟動時,幾經通過密鑰將密文解密,所以密鑰將稱為破譯關鍵,所以需要:
java -jar xxx.jar --jasypt.encryptor.password=xxx
將密鑰從項目中移除
更安全方案
1.擁有一個配置中心
2.配置中心有高度保密的配置文件
3.各項目向中心注冊公鑰,加密傳輸
4。項目內部通過私鑰解密
常見問題
1.
jasypt.encryptor.password: demo缺失
java.lang.IllegalStateException: Required Encryption configuration property missing: jasypt.encryptor.password
