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
相关推荐
砍材农夫13 小时前
python环境|conda安装和使用(1)
开发语言·后端·python·conda
用户2986985301413 小时前
Java 实践:查找与提取 Word 文档超链接
java·后端
Rust研习社13 小时前
Rust 错误处理的黄金搭档:一个定义错误,一个传播错误
后端·rust·编程语言
Flittly13 小时前
【AgentScope Java新手村系列】(9)SpringBoot集成
java·spring boot·spring
Moment13 小时前
从多人编辑到 Agent 写文档,Hocuspocus v4 正在改写协同系统 😍😍😍
前端·后端·面试
贺国亚13 小时前
评估-Eval-Hallucination与质量度量
后端·面试
Java内核笔记13 小时前
Spring Security 源码解析(五)表单登录认证全流程:UsernamePasswordAuthenticationFilter 拆解
后端
Dilee13 小时前
Spring AI 对话记忆:MessageChatMemoryAdvisor 最小接入
后端
星环科技13 小时前
数据标准Agent ,让企业数据说同一种语言
java·开发语言·前端
游码峰行13 小时前
游戏脚本挂攻防-在PoW中实现动态Hash策略及应用实践
后端