起因:因为我个人微博想要公开源码,但数据库配置文件会暴露在外面,又不想生产跟开发环境建来回切换,所以想到了加密数据库密码,于是问了群里的一个朋友小XX(原谅我不识字)
经过:
简单粗暴,直接上代码:
1.需要引入依赖
com.github.ulisesbocchio
jasypt-spring-boot-starter
2.0.0
2.然后再yml或者properties中加入:
jasypt.encryptor.password=nmyswls
这个是盐值
3.在测试类中
@RunWith(SpringRunner.class)
@SpringBootTestpublic classtest_jiami {
@Autowired
StringEncryptor stringEncryptor;
@Testpublic voidencryptPwd() {
String result= stringEncryptor.encrypt("root");
System.out.println(result);
}
}
这里面的参数是你的密码,然后返回来的是加密后的密码:yyrqk9reuY5kScmS3D9mWQ==
4.将加密后的密码贴到配置文件中,如下:
spring.datasource.password=ENC(yyrqk9reuY5kScmS3D9mWQ==)
这样就ok了