官网:https://github.com/ulisesbocchio/jasypt-spring-boot
中文 https://www.5axxw.com/wiki/content/3cyz9l
集成步骤- 导入依赖
com.github.ulisesbocchio jasypt-spring-boot-starter 3.0.4
- 根据盐值和算法生成密文
生成方法主要有以下途径(选择其一即可):- 运行jar生成
- 代码实现生成
- maven 插件生成
2.1 jar包执行命令生成方法(方法一)
cd到步骤一下载的maven jar包下面(如我的:D:mavenRepositoryorgjasyptjasypt1.9.3)
账号加密 root
D:mavenRepositoryorgjasyptjasypt1.9.3>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=security algorithm=PBEWithMD5AndDES ----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 11.0.14+8-LTS-263 ----ARGUMENTS------------------- # 需要加密的账号明文 input: root # 盐值 password: security # 指定的算法 algorithm: PBEWithMD5AndDES ----OUTPUT---------------------- # 加密后的密文 1xLTGeVebeMzt2xHBdnlVw==
密码加密 123456
D:mavenRepositoryorgjasyptjasypt1.9.3>java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=security algorithm=PBEWithMD5AndDES ----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 11.0.14+8-LTS-263 ----ARGUMENTS------------------- # 需要加密的密码明文 input: 123456 # 盐值 password: security # 指定的算法 algorithm: PBEWithMD5AndDES ----OUTPUT---------------------- # 加密后的密文 BvkV/QUwVuoRUQJN+HM5bg== D:mavenRepositoryorgjasyptjasypt1.9.3>
2.2 代码的方法实现(方法二)
package com.st.encriytion;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.stream.Stream;
@SpringBootTest
class EncryptionDemoApplicationTests {
private static final String ALGORITHM_INFO = "PBEWithMD5AndDES";
private static final String PASSWORD_INFO = "security";
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
//配置文件中配置如下的算法
standardPBEStringEncryptor.setAlgorithm(ALGORITHM_INFO);
//配置文件中配置的password
standardPBEStringEncryptor.setPassword(PASSWORD_INFO);
//要加密的文本
String name = standardPBEStringEncryptor.encrypt("root");
String password = standardPBEStringEncryptor.encrypt("123456");
String redisPassword = standardPBEStringEncryptor.encrypt("123456");
//将加密的文本写到配置文件中
System.out.println("name=" + name);
System.out.println("password=" + password);
System.out.println("redisPassword=" + redisPassword);
//要解密的文本
String name2 = standardPBEStringEncryptor.decrypt("FarrmxSQX5uwtH/NZRxy+g==");
String password2 = standardPBEStringEncryptor.decrypt("vhiaYB1gl9zPj16yu7uMkA==");
String redisPassword2 = standardPBEStringEncryptor.decrypt("ZII7UphhbVuJ8c3oxPUeyw==");
//解密后的文本
System.out.println("name2=" + name2);
System.out.println("password2=" + password2);
System.out.println("redisPassword2=" + redisPassword2);
}
}
2.3 maven 插件生成(方法三)
引入插件依赖
com.github.ulisesbocchio jasypt-maven-plugin 3.0.4
2.3.1 运行构建语句(单条构建)
# 执行的maven命令:mvn jasypt:encrypt-value -Djasypt.encryptor.password="security" -Djasypt.plugin.value="123456" D:javazimugeencryption-demo>mvn jasypt:encrypt-value -Djasypt.encryptor.password="security" -Djasypt.plugin.value="123456" [INFO] Scanning for projects... [INFO] [INFO] -----------------< com.st.encriytion:encryption-demo >------------------ [INFO] Building encryption-demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- ........... [INFO] # 生成的密文 ENC(5EGcahJ4dwaA7AnXVN1DJQ==) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.690 s [INFO] Finished at: 2022-05-16T16:52:46+08:00 [INFO] ------------------------------------------------------------------------
命令解析
mvn jasypt:encrypt-value -Djasypt.encryptor.password="security" -Djasypt.encryptor.algorithm="PBEWithMD5AndDES" -Djasypt.plugin.value="123456" # jasypt.encryptor.password,告诉Jasypt你提供的盐值。 # jasypt.encryptor.algorithm,告诉Jasypt用哪个算法加密。 # jasypt.plugin.value,告诉Jasypt加密的值什么。
2.3.2 根据配置文件批量生成
需求:我现在需要同时将mysql redis等所有的账户和密码都加密
定义一个application.properties文件,将需要加密的账号和密码都定义在这个文件中
sensitive.mysql.password=DEC(123456) sensitive.mysql.root=DEC(root) sensitive.redis.name=DEC(redisRoot) sensitive.redis.password=DEC(123456
修改
jasypt:decrypt -Djasypt.encryptor.password="security" -Djasypt.encryptor.algorithm="PBEWithMD5AndDES" -Djasypt.plugin.path="file:src/main/resources/application.properties"
或者直接运行下面的命令
mvn jasypt:decrypt -Djasypt.encryptor.password="security" -Djasypt.encryptor.algorithm="PBEWithMD5AndDES" -Djasypt.plugin.path="file:src/main/resources/application.properties"
查看之前的application.properties
会自动把加密后的密文覆盖之前的明文
sensitive.mysql.password=ENC(6XXMuBZmU29eQ6msrsajlQ==) sensitive.mysql.root=ENC(xRFskvmGm93lKsX6cv8oGA==) sensitive.redis.name=ENC(rCrUYUXBhJfJRNEZB8M3LWd5Gk7dTAmm) sensitive.redis.password=ENC(p+Iti3nBHwmno5uMVCURfQ==)
- 在application.yml文件中配置
spring:
datasource:
username: ENC(1xLTGeVebeMzt2xHBdnlVw==)
password: ENC(BvkV/QUwVuoRUQJN+HM5bg==)
url: jdbc:mysql://119.3.105.108:3306/st_i18n?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useAffectedRows=true&allowMultiQueries=true
driver-class-name: com.mysql.jdbc.Driver
# 加解密秘钥
jasypt:
encryptor:
# 生成密文时指定的盐值
password: security
# 生成密文时指定的算法
algorithm: PBEWithMD5AndDES
# 不同版本jasypt的默认iv-generator-classname不同,具体根据官网的版本指定,当前我的是最新的3.0.4
iv-generator-classname: org.jasypt.iv.NoIvGenerator
- 其他
不自定义加密类的话,默认算法为 PBEWithMD5AndDES
多次生成,
每次生成的密码不一样。不同的密码序列,解密却可以一样。
ENC前缀可改变,即自定义格式:需要添加配置
jasypt:
encryptor:
property:
prefix: "TEST["
suffix: "]"
rg.jasypt.iv.NoIvGenerator
4. 其他
不自定义加密类的话,默认算法为 PBEWithMD5AndDES
多次生成,
每次生成的密码不一样。不同的密码序列,解密却可以一样。
ENC前缀可改变,即自定义格式:需要添加配置
```yaml
jasypt:
encryptor:
property:
prefix: "TEST["
suffix: "]"



