1.springboot项目pom添加maven依赖
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
<groupid>com.github.ulisesbocchio</groupid>
<artifactid>jasypt-spring-boot-starter</artifactid>
<version>3.0.5</version>
</dependency>2.使用单元测试生成加密数据
设置配置文件(bootstrap.yml或者application.yml)
jasypt:
encryptor:
password: jasypt!di@soc
algorithm: pbewithmd5anddespassword:加密的盐algorithm:加密算法,这里使用 pbewithmd5anddes
运行单元测试文件
package com.pscsoft.code.isoc;
import org.jasypt.encryption.stringencryptor;
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.context.annotation.import;
/**
* @author avry.jiang
* @date 2023-06-28 17:45:58
*/
@springboottest()
@import({isocapplication.class})
public class jasyptutiltest {
@autowired
private stringencryptor encryptor;
@test
public void jasypt() {
string name = encryptor.encrypt("12345678");
system.out.println("en: " + name);
system.out.println("de: " + encryptor.decrypt(name));
}
}执行结果:

3.在springboot项目中使用
配置mysql的jdbc,使用“enc(密文)”配置密码,其他配置有密码都类似修改

4.将加密盐放入配置文件、启动环境或者启动命令中
4.1.idea启动项配置
--jasypt.encryptor.password=jasypt!di@soc --jasypt.encryptor.algorithm=pbewithmd5anddes

4.2.配置文件(不推荐,会暴露加密盐反推明文的)
jasypt:
encryptor:
password: jasypt!di@soc
algorithm: pbewithmd5anddes4.3.其他
如果是docker启动,可以配置在dockerfile中指定启动命令
entrypoint ["java","-jar","-xms1024m", "-xmx1024m","--jasypt.encryptor.password=jasypt!di@soc","--jasypt.encryptor.algorithm=pbewithmd5anddes", "/xxx/xxx.jar"]
启动项目即可~
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论