第一、添加依賴
<dependency>
? ? ? ?<groupId>com.github.ulisesbocchio</groupId>
? ? ? ? <artifactId>jasypt-spring-boot-starter</artifactId>
? ? ? ? <version>2.1.1</version>
</dependency>
第二、application.yml 配置秘鑰
jasypt:
? ? encryptor:
? ? ? ? ? ? password: thisispassword
第三、編寫test文件生成密碼:
@Autowired
StringEncryptorencryptor;
@Test
public void contextLoads() {
? ? ? ? ?String url =encryptor.encrypt("jdbc:mysql://220.12.23.17:3306/tools-test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false");
? ? ? ? ?String name =encryptor.encrypt("rootdb");
? ? ? ? ?String password =encryptor.encrypt("password2018");
? ? ? ? ?System.out.println("url:"+url);
? ? ? ? ?System.out.println("name:"+name);
? ? ? ? ?System.out.println("password:"+password);
}
輸出結(jié)果:
url:qMwOuqrSfWKKD2EC8CUHQVyc9WZu7vD5QrPLZhaJqV8sCLBhhiiqV/oh7JBsn0L4iHhkH4PcyWcdKkjYbsptGPDaWoRZdmDTMCUMo5aFOzZ+4PMJWAupt1XoBfbPEuka0YL01MlDM9RNLlAWUrzI19H2nsnt07bmqx5WRaA1AdTYdyCpn
username: XzQi3cMJA2kUybaQeFix
password: yL9y4+MXd5yzaO0U2X7FXclDMe5/Ra5ZxCMTQmkc
第四、將生成的秘鑰填充到application.yml文件里, 注意前面需要添加ENC,比如:
? ??username: ENC(XzQi3cMJA2kUybaQeFixxQ==)
? ??password: ENC(yL9y4+MXd5yzaO0U2X7FXclDMe5/Ra5ZxCMTQmkgwoc=)
第五、編寫make文件
? ?? mvn clean package -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true
?第六、刪除application.yml 文件里的配置秘鑰
?第七、 執(zhí)行make文件 生成target jar
?第八、java -jar demo-jasypt-0.0.1-SNAPSHOT.jar? --jasypt.encryptor.password=thisispassword
注意:?
? ? 秘鑰盡量不要放在配置文件里。在啟動app的時候添加上去。
? 以上是我生產(chǎn)中用到的一點知識,與大家共享。如有技術(shù)問題,請給我留言。