配置文件的属性ENC加密
转载:https://www.cnblogs.com/zqyx/p/9687136.html
在micro service体系中,有了config server,我们可以把配置存放在git、svn、数据库等,普通的web项目也基本上是把配置存放在配置文件中。如果我们把大量的配置信息都放在配置文件中是会有安全隐患的,那么如何消除这个隐患呢?最直接的方式就是把配置信息中的一些敏感信息(比如数据库密码、中间件密码)加密,然后程序在获取这些配置的时候解密,就可以达到目的。这个时候,jasypt框架就派上用场了。
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
- High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries...
- Transparent integration with Hibernate.
- Suitable for integration into Spring-based applications and also transparently integrable with Spring Security.
- Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
- Specific features for high-performance encryption in multi-processor/multi-core systems.
- Open API for use with any JCE provider.
- ...and much more
意思其实就是可以把Jasypt这个Java库当成是一个黑盒子,无需深入了解里面怎么运作的,可以拿来直接用(当然,作为码农我们还是要去一探究竟的,个人兴趣自己看吧)。现在基本上项目都集成spring boot了,因此我们可以使用jasypt-spring-boot-starter。
1. 引入pom包
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
2. 找到maven仓库中的 jasypt-1.9.2.jar(路径是:.m2\repository\org\jasypt\jasypt\1.9.2)
3. 调用jasypt-1.9.2.jar包中一个main方法:org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI ,源码参考:

然后执行一下语句:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="test" password=salt algorithm=PBEWithMD5AndDES
-- input参数:你想要加密的密码
-- password参数:jasypt用来加密你的密码的密码
结果如下图,途中的output就是加密后的密文了。
4. 修改配置文件,把配置文件中要加密的字段改为:ENC(密文)
system: mysql: url: xxx.xxx.xx.xx username: username password: ENC(qh8kixDUkvm1DIJrpLFtzw==) jasypt: encryptor: password: salt
以上就可以达到目的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY