jasypt 敏感信息加密_fh
敏感信息加密
一般在信息安全要求比较高的项目中,一些敏感信息会被要求进行加密。如数据库、中间件等用户名密码。
1.pom.xml
<!-- 敏感信息加密 -->
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.application.yml
#对应着加密Util中设置的加密密码
jasypt:
encryptor:
password: mycode
- 1
- 2
- 3
- 4
3.生成密文
public class EncryptUtils {
/**
* 配置文件中配置的password
*/
public static final String PASSWORD = "mycode";
/**
* 生成加密数据
* @param key
*/
private static void doEncrypt(String key){
BasicTextEncryptor textEncryptor =new BasicTextEncryptor ();
/*配置文件中配置的password*/
textEncryptor.setPassword(PASSWORD);
/*要加密的文本*/
String secrityKey = textEncryptor.encrypt(key);
/*将加密的文本写到配置文件中*/
log.info("{}:ENC({})",key,secrityKey);
}
/**
* 生成加密
* @param args
*/
public static void main(String[] args) {
// 数据库账号
EncryptUtils.doEncrypt("root");
// 数据库密码
EncryptUtils.doEncrypt("123456");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
4.将生成的密文替换明文
username: ENC(DU3waYln9xg3rGHTk1Krrg6WkSVkmBTn)
password: ENC(UXNUuHhgYxG3gChR/FubgEQAkMyYGQaP)
- 1
- 2
推荐阅读