spring如何对数据库密码进行加密
时间: 2023-05-14 11:05:59 浏览: 126
Spring可以使用Jasypt库对数据库密码进行加密。首先,需要在pom.xml文件中添加Jasypt依赖:
```
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
```
然后,在application.properties文件中配置加密相关的属性:
```
jasypt.encryptor.password=your_secret_password
spring.datasource.password=ENC(encrypted_password)
```
其中,jasypt.encryptor.password是用于加密的密钥,your_secret_password是自定义的密钥,spring.datasource.password是加密后的数据库密码,ENC表示该密码已经被加密。
最后,需要在启动类上添加@EnableEncryptableProperties注解,启用加密功能。
```
@SpringBootApplication
@EnableEncryptableProperties
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
这样,Spring就会自动解密数据库密码并连接数据库。
阅读全文
相关推荐
















