jasypt加密配置文件

jasypt 加密配置文件

依赖

1
2
3
4
5
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class JasyptDemo {

public static void main(String[] args) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setAlgorithm("PBEWithMD5AndDES");
encryptor.setPassword("abc123qwer1113543");
encryptor.setIvGenerator(new RandomIvGenerator());
encryptor.setSaltGenerator(new RandomSaltGenerator());
encryptor.setStringOutputType("hexadecimal");

String encryptText = encryptor.encrypt("123456");
log.info("加密: {}", encryptText);

String decryptText = encryptor.decrypt(encryptText);
log.info("解密: {}", decryptText);
}
}

# 加密: 5E8A4A737ECD564CD00DAF5E88FEA28662C32943C34065D9
# 解密: 123456

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
jasypt:
encryptor:
algorithm: "PBEWithMD5AndDES"
iv-generator-classname: "org.jasypt.iv.RandomIvGenerator"
salt-generator-classname: "org.jasypt.salt.RandomSaltGenerator"
string-output-type: "hexadecimal"
property:
prefix: "ENC("
suffix: ")"

spring:
data:
redis:
host: localhost
port: 6379
password: "ENC(5E8A4A737ECD564CD00DAF5E88FEA28662C32943C34065D9)"

设置密码并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
java -Djasypt.encryptor.password=password -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar

or

java -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar --jasypt.encryptor.password=password

or

jasypt:
encryptor:
password: ${JASYPT_ENCRYPTOR_PASSWORD:}

export JASYPT_ENCRYPTOR_PASSWORD=password
java -jar jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar