1.SpringBoot项目pom添加maven依赖
com.github.ulisesbocchio jasypt-spring-boot-starter 3.0.5
2.使用单元测试生成加密数据
设置配置文件(bootstrap.yml或者application.yml)
jasypt: encryptor: password: jasypt!di@soc algorithm: PBEWithMD5AndDES
password:加密的盐
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: PBEWithMD5AndDES
4.3.其他
如果是docker启动,可以配置在Dockerfile中指定启动命令
ENTRYPOINT ["java","-jar","-Xms1024m", "-Xmx1024m","--jasypt.encryptor.password=jasypt!di@soc","--jasypt.encryptor.algorithm=PBEWithMD5AndDES", "/xxx/xxx.jar"]
启动项目即可