Spring boot敏感参数加密配置

一,背景

在项目中很多参数会被配置到配置文件中,比如说密钥,用户名,数据库连接,账号密码之类的,如果用明文配置,会有一定的安全风险。为了减小风险,增加对敏感配置数据的加密配置。

二,实现步骤

1. 引入maven依赖

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk18on</artifactId>
  <version>1.78.1</version>
</dependency>


2. Application启动类配置

@EnableEncryptableProperties


3. bootstrap.yml配置

# 配置文件加解密配置
jasypt:
  encryptor:
    # 加解密算法
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
    property:
      # 算法识别前缀(当算法发现配置文件中的值以这前缀开始,后缀结尾时,会使用指定算法解密)
      prefix: ENC(
      # 算法识别后缀
      suffix: )


4. 加解密工具类

package com.xxx.xxx;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

public class JasyptUtils {
    public static final String PASSWORD = "abc";
    public static final String ALGORITHM = "PBEWithMD5AndDES";

    public static void main(String[] args) {
        process("root");
        process("123456");
    }

    /**
     * 加密
     *
     * @param plaintext 明文
     * @return
     */
    public static String encrypt(String plaintext) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        // 指定算法
        config.setAlgorithm(ALGORITHM);
        // 指定秘钥,和yml配置文件中保持一致
        config.setPassword(PASSWORD);
        encryptor.setConfig(config);
        // 生成加密数据
        return encryptor.encrypt(plaintext);
    }

    /**
     * 解密
     *
     * @param data 加密后数据
     * @return
     */
    public static String decrypt(String data) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        // 指定算法
        config.setAlgorithm(ALGORITHM);
        // 指定秘钥,和yml配置文件中保持一致
        config.setPassword(PASSWORD);
        encryptor.setConfig(config);
        // 解密数据
        return encryptor.decrypt(data);
    }

    private static void process(String text) {
        String ciphertext = encrypt(text);
        System.out.println("ciphertext: " + ciphertext);
        System.out.println("plaintext: " + decrypt(ciphertext));
        System.out.println("--------------------------");
    }
}


5. 将上一步得到加密后的文本写入bootstrap.yml

密文用指定的前后缀包围: ENC(xxx)
spring:
  # 数据源
  datasource:
    url:         ENC(Pd+8gi8reZ8qAWQmlyqeSGUfbE9McRBBADMLHOPefTQZUKGUGqmyEK8Zfk/McQZuBkku1/ukvFpoimbZ23GmBwGx3P2/LxYHUMObbBN1qj5wFlVAENDB50Or43R9oKjgbfeGgZ+v5hFP3EsUzJoBW/fVt9Wv0QLKRZ3FgZl+/1ibqdI4HON7T1cV7xtOQrcY7yKxZfozalY0jN7paaUznx6pidmjE1zLAOpfzigzxlIhcBiQeKqKfQzZLk2LSODG)
    username: ENC(qFu6FeDJrXw3FV0Aqty2aw==)
    password: ENC(mimO0LHX+2DzbXLZKeoXPVXVw9uzGehK)


6. 在启动命令增加秘钥环境变量

密钥: abc

java  -Djasypt.encryptor.password=abc -jar xxx.jar
注意: 秘钥不可配置在bootstrap.yml, 可以放在服务器的指定位置,或者嵌入到K8s的secrect config里面。

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东皋长歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值