基于Jasypt对SpringBoot配置文件加密
作者:风静花犹落 时间:2023-07-13 20:13:03
这篇文章主要介绍了基于Jasypt对SpringBoot配置文件加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
引入jasypt
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
生成要加密的字符串
将数据库的用户名和密码进行加密
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐)
textEncryptor.setPassword("G0CvDz7oJn6");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("root123");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
输出信息为:
username:i8QgEN4uOy2E1rHzrpSTYA==
password:6eaMh/RX5oXUVca9ignvtg==
或者使用Maven下载好的jar包加密\Maven\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=G0CvDz7oJn6 algorithm=PBEWithMD5AndDES input=root
输出信息为:
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11
----ARGUMENTS-------------------
input: root
algorithm: PBEWithMD5AndDES
password: G0CvDz7oJn6
----OUTPUT----------------------
Gvkoz+sbFWiRe3ECtizV1A==
拷贝-OUTPUT-下的结果即可
配置properties文件
将生成的加密串配置ENC(加密串)到application.properties中
# 加密所需的salt(盐)
jasypt.encryptor.password=G0CvDz7oJn6
# 默认加密方式PBEWithMD5AndDES,可以更改为PBEWithMD5AndTripleDES
# jasypt.encryptor.algorithm=PBEWithMD5AndDES
spring.datasource.username=ENC(6eaMh/RX5oXUVca9ignvtg==)
spring.datasource.password=ENC(6eaMh/RX5oXUVca9ignvtg==)
加密方式对应的类为BasicTextEncryptor和StrongTextEncryptor
public BasicTextEncryptor() {
super();
this.encryptor = new StandardPBEStringEncryptor();
this.encryptor.setAlgorithm("PBEWithMD5AndDES");
}
public StrongTextEncryptor() {
super();
this.encryptor = new StandardPBEStringEncryptor();
this.encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
}
类图
部署时配置salt(盐)值
为了防止salt(盐)泄露,反解出密码.可以在项目部署的时候使用命令传入salt(盐)值
java -jar -Djasypt.encryptor.password=G0CvDz7oJn6 xxx.jar
或者在服务器的环境变量里配置,进一步提高安全性
打开/etc/profile文件
vim /etc/profile
文件末尾插入
export JASYPT_PASSWORD = G0CvDz7oJn6
编译
source /etc/profile
运行
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
官方地址 : https://github.com/ulisesbocchio/jasypt-spring-boot
来源:https://www.jianshu.com/p/323ec96c46d2
标签:jasypt,spring,boot,配置,文件,加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 绘制统计图大全(柱状图, 折线图, 扇形图)
2023-03-12 09:16:26
![](https://img.aspxhome.com/file/2023/0/108690_0s.png)
.NET/C#实现识别用户访问设备的方法
2021-12-20 06:30:58
java面向对象设计原则之里氏替换原则示例详解
2023-02-02 05:04:41
C#中函数的创建和闭包的理解
2022-08-17 01:14:35
![](https://img.aspxhome.com/file/2023/9/111109_0s.png)
谈谈Android的三种网络通信方式
2023-04-19 17:28:47
![](https://img.aspxhome.com/file/2023/5/99805_0s.png)
Android view自定义实现动态进度条
2023-09-16 00:40:38
![](https://img.aspxhome.com/file/2023/5/138125_0s.gif)
Java Fluent Mybatis 项目工程化与常规操作详解流程篇 上
2022-08-28 16:47:28
![](https://img.aspxhome.com/file/2023/0/100670_0s.png)
SpringBoot集成elasticsearch使用图文详解
2021-06-06 16:12:47
![](https://img.aspxhome.com/file/2023/0/70490_0s.png)
SpringBoot的异常处理流程是什么样的?
2021-07-09 17:54:40
![](https://img.aspxhome.com/file/2023/4/81894_0s.png)
c#爬虫爬取京东的商品信息
2022-12-03 14:38:11
FragmentStatePagerAdapter保存恢复下拉刷新Fragment内存数据
2023-07-17 20:36:40
Sonar编译问题对应:File [...] can't be indexed twice.
2023-11-25 11:07:28
![](https://img.aspxhome.com/file/2023/3/58903_0s.png)
关于java入门与java开发环境配置详细教程
2023-11-24 14:21:44
![](https://img.aspxhome.com/file/2023/8/59568_0s.png)
实例详解Android自定义ProgressDialog进度条对话框的实现
2023-02-18 08:32:40
![](https://img.aspxhome.com/file/2023/9/94999_0s.jpg)
java 引用传递的三种类型小结
2023-09-03 03:59:47
![](https://img.aspxhome.com/file/2023/0/60560_0s.png)
Java实现中英文词典功能
2021-06-20 18:25:56
C# 无边框窗体边框阴影效果的简单实现
2021-12-06 23:41:43
UnityShader3实现彩光效果
2021-09-02 03:48:53
![](https://img.aspxhome.com/file/2023/9/129659_0s.gif)
实例讲解Java并发编程之闭锁
2023-10-25 14:25:07
C语言实现通讯录小项目
2022-10-05 07:33:36
![](https://img.aspxhome.com/file/2023/7/124057_0s.jpg)