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

需求

华为安全测试要求配置文件中关键信息需要以密文存储如数据库密码、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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值