Spring Boot使用jasypt对配置文件中敏感信息进行加密
2020-09-01 17:24
232 查看
日常使用中,数据库、redis、kafka等信息一般会配在配置文件中,而且以明文的方式,这样就很不安全,容易造成重要信息的泄露。
正好之前我们做项目的时候用到 jasypt 进行加密存储。
最近需要修改数据库密码,正好记录一下这个知识点。
1、引入依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>1.16</version> </dependency>
2、加密
在 maven 仓库找到 jasypt 的 jar 包,
打开命令行窗口
命令为:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=123456 algorithm=PBEWithMD5AndDES input=lixj
- password:加密的私钥
- input:要加密的信息
如图所示,私钥为123456,lixj 加密后的密文为:resHmHRaVO6d7CcyJLHv8Q==
如果不喜欢可以执行多次,每次生成的密文都不一样。
3、配置
将加密后的信息配置在配置文件,使用 ENC 关键字。
添加 @EnableEncryptableProperties
应用启动时需要添加以下启动参数,不然会解密不出来,导致启动失败。
-Djasypt.encryptor.password=123456 -Djasypt.encryptor.algorithm=PBEWithMD5AndDES
4、解密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI password=123456 algorithm=PBEWithMD5AndDES input="resHmHRaVO6d7CcyJLHv8Q=="
5、java 代码实现加解密
import org.jasypt.intf.service.JasyptStatelessService; public enum JasyptEncryptUtil { /** * 唯一实例,使工具类单例化 */ INSTANCE; private static JasyptStatelessService jasyptStatelessService = new JasyptStatelessService(); // 加密 public static String encrypt(String input, String password, String algorithm) { return jasyptStatelessService.encrypt(input, password, null, null, algorithm, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); } // 解密 public static String decrypt(String input, String password, String algorithm) { return jasyptStatelessService.decrypt(input, password, null, null, algorithm, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,null); } /** * 使用默认加密算法:PBEWithMD5AndDES * 加密 * @param input 要加密的内容 * @param password 密钥 * @return */ public static String encrypt(String input, String password) { return encrypt(input, password, "PBEWithMD5AndDES"); } /** * 使用默认加密算法:PBEWithMD5AndDES * 解密 * @param input 要解密的内容 * @param password 密钥 * @return */ public static String decrypt(String input, String password) { return decrypt(input, password, "PBEWithMD5AndDES"); } public static void main(String[] args) { System.out.println(encrypt("lixj", "123456")); System.out.println(decrypt("9HhTbI9i6bh7D2tAVDYblA==", "123456")); } }
相关文章推荐
- SpringBoot配置文件敏感信息加密-jasypt
- 使用Jasypt对数据库配置文件进行加密
- Jasypt : 整合spring boot加密应用配置文件敏感信息
- 采用DES加密方式对properties配置文件敏感信息加密处理
- logstash 对配置文件conf敏感信息,密码等加密
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- 对Java配置文件中敏感信息进行加解密的工具类
- Spring Boot下集成Jasypt对配置文件进行加密
- Spring Boot: 加密应用配置文件敏感信息
- Spring Boot: 加密应用配置文件敏感信息
- 对Java配置文件中敏感信息进行加解密的工具类
- Linux如何使用GPG(GNU Privacy Guard)对信息/文件进行加密和解密
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- 对Java配置文件中敏感信息进行加解密的工具类
- java 配置文件中敏感信息进行加解密的工具类
- SpringBoot配置文件敏感信息加密
- springboot项目使用jasypt实现自定义配置文件内容加密
- 【Java库】如何使用优秀的加密库Jasypt来保护你的敏感信息?
- Spring 中使用jasypt对配置文件(.properties)中密码加密
- jasypt 实现对配置文件中的数据密码进行加密