对SpringBoot项目配置文件进行加密实现过程
目录
- 1.SpringBoot项目pom添加maven依赖
- 2.使用单元测试生成加密http://www.devze.com数据
- 3.在SpringBoot项目中使用
- 4.将加密盐放入配置文件、启动环境或者启动命令中
- 4.1.Idea启动项配置
- 4.2.配置文件(不推荐,会暴露加密盐反推明文的js)
- 4.3.其他
- 总结
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>
<vepythonrsion>3.0.5</version>
</dependency>
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);
js 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"]
启动项目即可~
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
加载中,请稍侯......
精彩评论