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

编程入门 行业动态 更新时间:2024-10-26 08:34:25

Jasypt整合springboot完成对<a href=https://www.elefans.com/category/jswz/34/1770506.html style=配置文件中有关敏感数据的加密"/>

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

场景

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

步骤

1、pom文件引入依赖。

<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.4</version>
</dependency>

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

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

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

# Spring配置
spring:# 数据源配置datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&&serverTimezone=Asia/Shanghai&&useSSL=falseusername: root# 此处的密文要用ENC()进行包裹,不然是识别不了的,自然也就无法解密password: ENC(fUaD/g4L9TKx8AFhz8A5qQ==)# 加密配置
jasypt:encryptor:# 指定密钥password: haha# 下面这两个参数必须得加,不加的话会报参数绑定异常# 指定解密算法,需要和加密时使用的算法一致algorithm: PBEWithMD5AndDES# 指定initialization vector类型iv-generator-classname: org.jasypt.iv.NoIvGenerator

线上环境

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

java -jar -Djasypt.encryptor.password=your-secret

更多推荐

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

本文发布于:2023-11-30 13:37:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   敏感数据   Jasypt   springboot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!