在springboot中,配置數據庫等信息時,用戶名和密碼明文顯示會大大下降安全性,在此介紹一種加密方式,簡單易用。java
添加依賴:mysql
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.8</version> </dependency>
在yml文件或properties文件中配置加密參數:git
jasypt: encryptor: password: 123
獲得加密後的密碼:github
@Autowired StringEncryptor stringEncryptor; @Test public void encryptPwd() { String result = stringEncryptor.encrypt("yourpassword"); System.out.println(result); }
將加密後的密碼配置在yml或properties文件中便可:spring
datasource: url: jdbc:mysql://網段/數據庫名 username: 用戶名 password: ENC(Ipjb1cUctOHmbt6a1qIUjw==) #Ipjb1cUctOHmbt6a1qIUjw== 就是加密後的密碼 driverClassName: com.mysql.jdbc.Driver