1:導入依賴
<!-- 數據庫加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2:然後再yml或者properties中加入
這個是鹽值
jasypt.encryptor.password=nmyswls
3:在測試類中
@RunWith(SpringRunner.class)
@SpringBootTest
public class test_jiami {
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
String result = stringEncryptor.encrypt("root");
System.out.println(result);
}
}
這裏面的參數是你的密碼,然後返回來的是加密後的密碼:yyrqk9reuY5kScmS3D9mWQ==
4:將加密後的密碼貼到配置文件中,如下:
spring.datasource.password=ENC(yyrqk9reuY5kScmS3D9mWQ==)
這樣就ok了,但我感覺如果你一不小心將你的密碼在測試代碼中提交上去,豈不是白加密了嗎,其實還有一種方法:
在你的maven倉庫中找到:D:\repository\org\jasypt\jasypt\1.9.2,這個包就是加密的包了
cmd在這個包下執行如下命令,它會返回你加密後的密碼:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=zhang algorithm=PBEWithMD5AndDES
其中:
input:是數據庫的明文密碼
password:是機密的鹽
algorithm:是加密的方式(默認)
然後在配置文件中把鹽加進去就ok了
jasypt.encryptor.password=nmyswls
結果:媽媽再也不用擔心我的密碼被盜了~