Springboot配置文件内容加密代码实例
作者:donfaquir 时间:2022-09-13 05:56:09
这篇文章主要介绍了Springboot配置文件内容加密代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用的是jasypt-spring-boot-starter,具体介绍可以参考 https://gitee.com/yangziyi2017/Jasypt-Spring-Boot
引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
创建加密工具类
需要创建一个工具类,用于对明文进行加密,获取密文,然后把密文写入到application.yml这样的配置文件中
package com.hikvision.seclab.common.util.encrypt;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
/**
* 使用jasypt对配置文件进行加密的工具
* @author: 2019/11/7 16:47
* @since: 0.0.1-SNAPSHOT
* @modified By:
*/
public class JasyptTool {
public static void main(String[] a){
StandardPBEStringEncryptor se = new StandardPBEStringEncryptor();
se.setPassword("xxx");
String postgres = se.encrypt("abc123");
System.out.println(postgres);
}
}
其中有一个设定密码的动作,是设定加密使用的salt,时jasypt组件必选的配置,有些文章中在配置文件中使用jasypt.encryptor.password设置,这样不安全,别人拿到salt,使用jasypt可以直接解密配置文件中的密文。
修改配置文件
获取密文后,修改配置文件
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql:
spring.datasource.username=pg_name
spring.datasource.password=ENC(t78dKQb1viAT2QKDxxeerdaNm6wyDCJ)
spring.datasource.initialization-mode=always
设置jasypt.encryptor.password
开发环境
可以通过配置jvm启动参数来指定jasypt.encryptor.password的值
生产环境
可以在启动时,设定在启动参数中
java -Dfile.encoding=UTF8 -Djasypt.encryptor.password=e9fbdb2d3b21 -jar -Xmx512m xxxDemo.jar
在docker容器中密文的密码可以设置成环境变量(如:JASYPT_PASSWORD ),上述命令可以修改为:
java -Dfile.encoding=UTF8 -Djasypt.encryptor.password=${JASYPT_PASSWORD} -jar -Xmx512m settlement.jar
来源:https://www.cnblogs.com/donfaquir/p/11814127.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中List.of()和Arrays.asList()的区别及原因分析
IDEA如何进行全局搜索图文教程
![](https://img.aspxhome.com/file/2023/4/63684_0s.jpg)
关于Mybatis-Plus字段策略与数据库自动更新时间的一些问题
Android Camera+SurfaceView自动聚焦防止变形拉伸
![](https://img.aspxhome.com/file/2023/6/82396_0s.png)
浅析Java编程中枚举类型的定义与使用
SpringBoot中@Import注解如何正确使用
![](https://img.aspxhome.com/file/2023/7/57707_0s.png)
java递归实现拼装多个api的结果操作方法
C语言文件操作函数大全(超详细)
Spring Boot Redis 集成配置详解
java实现顺时针打印矩阵
Java maven三种仓库,本地仓库,私服,中央仓库的配置
![](https://img.aspxhome.com/file/2023/2/62132_0s.png)
Java基础之Thymeleaf的简单使用
![](https://img.aspxhome.com/file/2023/6/58086_0s.png)
基于java构造方法Vector修改元素源码分析
![](https://img.aspxhome.com/file/2023/7/60237_0s.png)
Dubbo retries 超时重试机制的问题原因分析及解决方案
详解IntelliJ IDEA中TortoiseSVN修改服务器地址的方法
![](https://img.aspxhome.com/file/2023/0/59490_0s.png)
Java截取字符串的方法
@Autowired注解注入的xxxMapper报错问题及解决
![](https://img.aspxhome.com/file/2023/5/63635_0s.jpg)
基于Mybatis-Plus的CRUD的实现
idea springboot远程debug的操作方法
![](https://img.aspxhome.com/file/2023/8/62498_0s.png)