springboot 整合 Jasypt 实现配置文件(application.yml/properties) 密文存储

文章介绍了如何在华为安全测试要求下,利用jasypt库在SpringBoot应用中对敏感信息如数据库密码进行加密存储。通过添加依赖、配置jasypt和自定义Bean,确保密码以密文形式存在于配置文件中,并在运行时自动解密。
摘要由CSDN通过智能技术生成

需求

华为安全测试要求配置文件中关键信息需要以密文存储如数据库密码、redis密码等

解决

1、POM文件

<!--`springboot使用2.2.5.RELEASE版本`-->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.5.RELEASE</version>
	<relativePath /> <!-- lookup parent from repository -->
</parent>

<!--`jasypt 使用3.0.2版本`-->
 <dependency>
      <groupId>com.github.ulisesbocchio</groupId>
      <artifactId>jasypt-spring-boot-starter</artifactId>
      <version>3.0.2</version>
 </dependency>

2.1、application.yml-jasypt配置

jasypt:
  encryptor:
    bean: encryptorBean
    password: encryptorPassword
    algorithm: PBEWithMD5AndDES

bean: 请注意,bean名称是必需的,因为从1.5版开始,jasypt-spring-boot通过名称检测自定义String Encyptor。
默认bean名称是:

jasyptStringEncryptor

password: 加密所需盐,用于解密使用,由于每次生成的密码不一样所以需要使用salt来保证唯一性
algorithm: 加密算法

2.2、application.yml-spring.datasource.password配置

spring:
  datasource:
  	password: ENC(BQOUmhVru6jYpyE2hcHbPuZ2P/2wMTac)

ENC(XXX) 是默认写法,XXX用于存放加密后的密码

将3.1或者3.2生成的密码粘贴到XXX位置即可

3 密钥的两种生成方式

3.1 在线网址生成

在线网址生成
在这里插入图片描述

3.2 通过官方提供客户端生成

通过下载jasypt-1.9.3-dist.zip生成密码
进入项目 bin 目录下执行 encrypt 脚本

H:\jasypt-1.9.3\bin>encrypt.bat password=r9#u9n6$_i@c)u algorithm=PBEWithMD5AndDES input=secretmima

----ENVIRONMENT-----------------

Runtime: Temurin OpenJDK 64-Bit Server VM 25.345-b01



----ARGUMENTS-------------------

input: password
algorithm: PBEWithMD5AndDES
password: r9#u9n6$_i@c)u



----OUTPUT----------------------

8jEUim8SsbcdV+n+zfNEeiQ8CUPbf/g1
  • password 对应的是2.1中的 password
  • algorithm 对应的是2.1中的 algorithm
  • input 对应的是需要加密的密码
  • output 则是加密后的密码

4、自定义配置 Bean

通过自定义配置bean来扩展和更安全的加密配置文件
@EncryptablePropertySource 用来指定哪些配置文件需要加密属性

同样还可以使用分组功能
@EncryptablePropertySources({@EncryptablePropertySource(“classpath:encrypted.properties”),@EncryptablePropertySource(“classpath:encrypted2.properties”)})

package com.edu;

import com.ulisesbocchio.jasyptspringboot.annotation.EncryptablePropertySource;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author: Lisy
 * @Date: 2023/02/08/15:45
 * @Description: 配置文件加解密配置
 * 在线加密网址:<a href="https://www.devglan.com/online-tools/jasypt-online-encryption-decryption">...</a>
 */
@Configuration
@EncryptablePropertySource(name = "ApplicationYml", value = "application.yml")
public class JasyptConfig {

    @Bean(name = "encryptorBean")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        // 这是加密的盐,建议直接硬编码,提高安全性
        config.setPassword("r9#u9n6$_i@c)u");
        // 加密算法
        config.setAlgorithm("PBEWithMD5AndDES");
        // key 迭代次数
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        // 提供方// 池大小
        config.setProviderName("SunJCE");
        // 随机盐生成器
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        // 加密后输出字符串编码方式
        encryptor.setConfig(config);
        return encryptor;
    }

}

4.1 为了安全起见,将盐硬编码在JasyptConfig 配置文件中

最终配置文件修改为如下所示

# 配置文件加解密配置
jasypt:
  encryptor:
    bean: encryptorBean

5、启动springboot 验证是否解密成功

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
配置文件中的数据库信息如何加密? 回答: 在Spring Boot项目中,可以使用jasypt来加密配置文件中的数据库信息。首先,需要在pom.xml文件中导入相关依赖,如下所示: ``` <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> ``` 接下来,在application.yml文件中配置数据库信息时,可以使用jasypt进行加密。例如,如果要加密数据库密码,可以在配置文件中使用加密后的密码,如下所示: ``` spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: myusername password: ENC(加密后的密码) ``` 这样,配置文件中的数据库密码就会被加密保护起来,提高了安全性。同时,可以在代码中使用jasypt提供的解密方法来获取真实的密码进行数据库连接操作。 #### 引用[.reference_title] - *1* *3* [SpringBoot配置文件application.yml数据库信息加密](https://blog.csdn.net/qq_45370028/article/details/123115895)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot 配置 application.yml连接MySQL数据库](https://blog.csdn.net/qq_45830969/article/details/122697715)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值