jasypt mysql 密码 enc_配置文件的属性ENC加密
时间: 2023-11-12 22:06:51 浏览: 283
使用Jasypt对MySQL密码进行加密,可以将其加密后的值存储在配置文件中,以确保密码的安全性。下面是一个示例:
1. 在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
```
2. 在application.properties文件中添加以下属性:
```
# MySQL数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
# 加密后的密码,前缀为ENC
spring.datasource.password=ENC(encryptPassword)
# Jasypt配置
jasypt.encryptor.password=jasyptPassword
```
3. 创建一个加密工具类,用于执行加密操作:
```
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class JasyptEncryptor {
private final StringEncryptor stringEncryptor;
@Autowired
public JasyptEncryptor(StringEncryptor stringEncryptor) {
this.stringEncryptor = stringEncryptor;
}
public String encrypt(String input) {
return stringEncryptor.encrypt(input);
}
public String decrypt(String encryptedMessage) {
return stringEncryptor.decrypt(encryptedMessage);
}
}
```
4. 在应用程序中使用加密工具类来加密密码:
```
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyApp implements CommandLineRunner {
private final JasyptEncryptor jasyptEncryptor;
@Autowired
public MyApp(JasyptEncryptor jasyptEncryptor) {
this.jasyptEncryptor = jasyptEncryptor;
}
@Override
public void run(String... args) {
String password = "myPassword";
String encryptedPassword = jasyptEncryptor.encrypt(password);
System.out.println("Encrypted Password: " + encryptedPassword);
}
}
```
在此示例中,我们使用JasyptEncryptor类对密码进行加密,并将加密后的值存储在application.properties文件中。在应用程序中,我们可以使用JasyptEncryptor类来加密和解密密码,并将其用于数据库连接。
阅读全文
相关推荐


















