Springboot配置文件Properties密码加密

1. 添加依赖

<dependency>
   <groupId>com.github.ulisesbocchio</groupId>
   <artifactId>jasypt-spring-boot-starter</artifactId>
   <version>3.0.3</version>
</dependency>

2. 启动类添加注解

@EnableEncryptableProperties
在main里添加(因为仍然带着password关键字)
System.setProperty("jasypt.encryptor.password", "xxxxx");
System.setProperty("jasypt.encryptor.algorithm", "xxxxx");
SpringApplication.run(UranusApplication.class, args);
注意:jasypt-spring-boot-starter的2.x版本angorithm默认是PBEWithMD5AndTripleDES 3.x默认是PBEWITHHMACSHA512ANDAES_256

或者在启动时添加参数

java -jar xxx.jar -Djasypt.encryptor.password=私钥

3. 下载jasypt,在MvnRepository里搜索org.jasypt,下载1.9.3版本jasypt-1.9.3.jar

 

4. 生成ENC:在终端

java -cp /path/jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI encrypt input='xxxx' password=xxxx algorithm=PBEWithMD5AndTripleDES saltGeneratorClassName=org.jasypt.salt.RandomSaltGenerator ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator keyObtentionIterations=1000 stringOutputType=base64
注意1:此版本的主类是JasyptPBEStringEncryptionCLI
注意2: input输入密码
注意3: algorithm=PBEWithMD5AndTripleDES与代码里的保持一致

在Properties配置文件需要密码的地方

spring.datasource.password=ENC(xxxxxx)

 

posted @   jason47  阅读(606)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示