为加强配置文件里用户名、 密码的保密性,对配置文件进行加密(application.yml)
实现步骤:
1. 导入jar
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2. 配置加密时要使用的密钥
在yml 文件添加:
jasypt:
encryptor:
password: erp
3.生成密钥
@SpringBootTest
@Slf4j
public class PasswordEncondeTest {
@Autowired
private StringEncryptor stringEncryptor;
@Test
@DisplayName(value = "加密字符串")
public void test(){
log.info("password:{}",stringEncryptor.encrypt("1234561212"));
log.info("解密:{}",stringEncryptor.decrypt("tgbGCkkwDVmbrvon02T4F3JL+viYrRNEEQOGE"));
log.info("root password:{}",stringEncryptor.encrypt("root111"));
}
}
4. yml配置
spring:
datasource:
username: ENC(52rBUePp9pBF9ic4uYA62y6NG3TujEeVaGYxMQYz6gEYuWZpWKzRdaAV8xr+8ENH)
password: ENC(0eIfTbkKqDo7jsxrnPdBaLD8i4vKANY0L1hV+j/2V3qgAVriwgxHbvHCOwYbUB+g)
url: jdbc:mysql://XXXX:3306/XXXX?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver
参考文档: https://www.geeksforgeeks.org/how-to-encrypt-passwords-in-a-spring-boot-project-using-jasypt/