Jasypt整合springboot完成对配置文件中有关敏感数据的加密

场景

项目中有很多密码都是以明文的形式存储在配置文件中,这样很不安全。我们可以通过jasypt来完成敏感信息的加密。

步骤

1、pom文件引入依赖。

xml 复制代码
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>

2、写个测试类获取密文。

java 复制代码
        String secret = "haha";
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(secret); //设置密钥
        String s = basicTextEncryptor.encrypt("123456"); //加密
        System.out.println(s);

3、application.yml文件写死密文。

yaml 复制代码
# Spring配置
spring:
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&&serverTimezone=Asia/Shanghai&&useSSL=false
    username: root
    # 此处的密文要用ENC()进行包裹,不然是识别不了的,自然也就无法解密
    password: ENC(fUaD/g4L9TKx8AFhz8A5qQ==)

# 加密配置
jasypt:
  encryptor:
    # 指定密钥
    password: haha
    # 指定解密算法,需要和加密时使用的算法一致
    algorithm: PBEWithMD5AndDES
    # 指定initialization vector类型
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

线上环境

如果是线上环境,密钥需作为启动参数加进去,这样更安全。

powershell 复制代码
java -jar -Djasypt.encryptor.password=your-secret
相关推荐
爱学习的程序媛1 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
GISer_Jing2 小时前
Agent核心内容
后端·ai·langchain
budingxiaomoli3 小时前
Spring统一功能处理
java·后端·spring
某鹏4 小时前
网关选型:Go、Java、Graal 谁都不能完胜
后端
用户0247836530974 小时前
从屎山到防线:14 行状态迁移表,给任务系统装上防瞎跳闸门
后端
CQU_JIAKE4 小时前
11.13[体系结构】指标,五级流水线,八级流水线,虚实地址思考
后端
Flynn Liang4 小时前
可观测性与成本治理:一次对话的 17 个观测点
java·langchain·可观测性·micrometer·langchain4j·token 成本·llm 监控
金銀銅鐵5 小时前
带您看懂字节码指令(三):同一行加法,为什么有四种指令
java·jvm
panbook6 小时前
从0到1实现一个分布式时序数据库(三):主从复制、故障检测与切换
后端
上线再说6 小时前
图片从 8.2MB 压到 296KB:我把小程序图片链路的账单拆了一遍
后端