【SpringBoot学习】24、SpringBoot 集成 Jasypt 实现敏感信息加密解密

一、数据库账号密码加密

通常情况下,我们的数据库账号密码都是直接写在配置文件中的,这种写法实际是非常不安全的,当别人拿到项目配置文件之后,就能直接远程登录连接数据库,下面分析一下怎么针对数据库关键信息进行加密操作

(1)Jasypt 加密库

Java 库-Jasypt,全称为 Java Simplified Encryption,用于加密解密。它能够让开发者用花费最小的工作而把加密集成到项目中,并且不需要对加密/解密有深入的了解。

(2)项目中配置使用 Jasypt 库

首先需要引用相关依赖到项目中,我这里直接以 SpringBoot 为例,引入依赖如下

<!-- 数据库账户加密 -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

这里我没有采用最新版本,因为在使用上有一定差异

在 application.yml 配置文件中增加以下配置

# 数据库加密工具
jasypt:
  encryptor:
    password: TELLSEASALT # 盐值

创建一个普通的 java main 方法,用于生成加密信息

import org.jasypt.util.text.BasicTextEncryptor;

/**
 * Jasypt加密库
 *
 * @author Tellsea
 * @date 2021/02/28
 */
public class JasyptTest {

    public static void main(String[] arg) {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //1.加密所需的salt(盐),配置值jasypt.encryptor.password
        textEncryptor.setPassword("TELLSEASALT");
        System.out.println("加密账号:" + textEncryptor.encrypt("root"));
        System.out.println("加密密码:" + textEncryptor.encrypt("123456"));
    }
}

直接结果如下,生成加密后的信息

加密账号:DIeS/Bxlt7WmSnxNyC27gg==
加密密码:mcwxAAsJ8Ii62wxk5GegjA==

然后将加密后的信息填写在需要替换的明文处,默认使用 ENC() 包括加密字符串

# mysql
spring:
  datasource:
    druid:
      username: ENC(2WKbOhyeCzhiLVq4E9KUnA==)
      password: ENC(6QcZNCQYmtdynSKxxwagOw==)

加密字符串的前缀后缀可以自行配置

# 数据库加密工具
jasypt:
  encryptor:
    property:
      prefix: AAA( # 配置前缀
      suffix: )BBB # 配置后缀

常用其他配置,统一配置前缀为 jasypt.encryptor,相关配置的详细信息可以在源码的 JasyptEncryptorConfigurationProperties 类中查看,由于版本的不同,需要注意一下可能默认值会不同

配置项必须Default Value
jasypt.encryptor.passwordTrue-
jasypt.encryptor.algorithmFalsePBEWithMD5AndDES
jasypt.encryptor.keyObtentionIterationsFalse1000
jasypt.encryptor.poolSizeFalse1
jasypt.encryptor.providerNameFalseSunJCE
jasypt.encryptor.providerClassNameFalsenull
jasypt.encryptor.saltGeneratorClassnameFalseorg.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.ivGeneratorClassnameFalseorg.jasypt.iv.RandomIvGenerator
jasypt.encryptor.stringOutputTypeFalsebase64
jasypt.encryptor.proxyPropertySourcesFalsefalse

(3)对盐值进行深度保护

综合来说解决了敏感信息加密的问题,但是伴随着一个新的问题出现,盐值是存放在 application.yml 配置文件中的,同样是不安全的,可以说是没有彻底解决加密问题,下面我们继续修改配置解决这个问题

先我们删除 application.yml 配置文件中jasypt.encryptor属性

下面有三种方式来解决这个问题(推荐第三种):

  • 第一种:JVM 参数

在启动项目时,增加参数:-Djasypt.encryptor.password=TELLSEASALT

  • 第二种:服务器的环境变量

第二种,将盐值配置成服务器的系统环境变量,启动时获取参数,只有拿到服务器权限的人,才能查看到盐值在哪

# 新增变量
export JASYPT_PASSWORD = TELLSEASALT

# 使配置文件生效
source /etc/profile

# 启动项目时,增加启动参数配置
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
  • 第三种:使用自定义的 Encryptor 来存放

增加一个自定义配置类

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 Tellsea
 * @date 2021/03/01
 */
@Configuration
public class EncryptorConfig {

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("TELLSEASALT");
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.salt.NoOpIVGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }
}

到此,数据库账号密码加密已经完成!

技术分享区

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tellsea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值