SpringBoot项目配置文件数据库用户名密码加密

1、需求

在使用SpringBoot开发过程中,会将一些敏感信息配置到SpringBoot项目的配置文件中(不考虑使用配置中心的情况 ),例如数据库的用户名和密码、Redis的密码等。为了保证敏感信息的安全,我们需要将此类数据进行加密配置。

2、操作步骤

2.1 添加依赖

目前通用的做法是使用 jasypt 对数据库用户名或者密码进行加密,在springboot项目的POM中添加如下依赖,目前最新的版本是3.0.3,但是我们不用最新版本,而是使用2.1.2版本。后边会说明原因。

xml 复制代码
<!--数据库密码加密依赖-->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>

2.2 生成密文

依赖添加之后,会在你本地的maven仓库中下载相关的依赖包,进入到你自己的maven仓库,通过路径/org/jasypt/jasypt/1.9.3,找到 jasypt-1.9.3包。

  • Windows系统 直接在文件地址栏输入cmd进入到命令行。
  • Mac系统通过Terminal终端进入到对应路径即可。

如下命令:

bash 复制代码
java -cp  jasypt-1.9.3.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=helloWorld algorithm=PBEWithMD5AndDES
  • input:输入内容,此处指的是数据库密码。
  • password:不是指的密码,而是加密所使用的"盐",可以随便定义。
  • algorithm:加密所使用的算法,非必填,此处使用"PBEWithMD5AndDES" 算法。

如果想了解其他配置可以查看如下配置类:

bash 复制代码
com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties

执行命令显示如下信息:

bash 复制代码
>java -cp  jasypt-1.9.3.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=helloWorld algorithm=PBEWithMD5AndDES

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

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.381-b09



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

algorithm: PBEWithMD5AndDES
input: root
password: helloWorld



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

1vAgkftBPnIYh/gjCokbFA==

OUTPUT即为加密后输入的内容。

同样的操作,将"input"改为数据库密码如"123"再执行一次操作。

bash 复制代码
>java -cp  jasypt-1.9.3.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123" password=helloWorld algorithm=PBEWithMD5AndDES

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

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.381-b09



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

algorithm: PBEWithMD5AndDES
input: 123
password: helloWorld



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

LPbn37xuIIAfCkaermp5cQ==

OUTPUT即为密码加密后的数据。

2.3 修改Springboot 数据库配置

生成加密密文后,修改项目数据库连接的用户名和密码:

yaml 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true
    username: ENC(1vAgkftBPnIYh/gjCokbFA==)
    password: ENC(LPbn37xuIIAfCkaermp5cQ==)

此时,已经配置完成。

2.4 SpringBoot项目配置文件中配置"加密盐"

为了能够使程序获取我们加密前的数据,需要在项目配置文件中配置"加密盐",即2.2节中所说的"password",如下所示:

yaml 复制代码
jasypt:
  encryptor:
    password: helloWorld

但是在配置文件中配置加密盐也是不安全的,如果别人知道了加密后的密文,又知道了加密盐,就可以通过如下解密命令进行解密,这样就会导致我们的密码泄露。

bash 复制代码
>java -cp  jasypt-1.9.3.jar  org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="LPbn37xuIIAfCkaermp5cQ==" password=helloWorld algorithm=PBEWithMD5AndDES

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

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.381-b09



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

algorithm: PBEWithMD5AndDES
input: jeo6NNy5rK0x3rDkywsvBw==
password: esunny_Qwer2023



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

123

在本地开发时,可以这样操作,或者通过配置idea的虚拟机参数也是可以的

上线操作时通过增加启动参数进行配置,以保证安全。

3、问题解答

在2.1节添加依赖中我们使用的 2.1.2版本,而没有使用最新的3.0.3版本。本人亲测,在使用3.0.3版本时,按照以上配置完成后启动项目,会出新如下报错信息:

bash 复制代码
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource.dynamic.datasource.master.password' to java.lang.String:

    Reason: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.dynamic.datasource.master.password' to java.lang.String

Action:

Update your application's configuration

Disconnected from the target VM, address: '127.0.0.1:56043', transport: 'socket'

较新的版本更改了相关算法,会出现如上错误,降低版本即可。

注意: 如果使用了2.1.2版本,但是没有配置 加密盐,也会报上边的错误,按照如上配置加密盐即可。

相关推荐
一心只为学7 分钟前
Oracle密码过期问题,设置永不过期
数据库·oracle
小光学长15 分钟前
基于vue框架的宠物销售管理系统3m9h3(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
数据库
wn53122 分钟前
【Go - 类型断言】
服务器·开发语言·后端·golang
bjzhang7533 分钟前
SpringBoot开发——集成Tess4j实现OCR图像文字识别
spring boot·ocr·tess4j
flying jiang38 分钟前
Spring Boot 入门面试五道题
spring boot
小菜yh39 分钟前
关于Redis
java·数据库·spring boot·redis·spring·缓存
希冀1231 小时前
【操作系统】1.2操作系统的发展与分类
后端
Microsoft Word1 小时前
数据库系统原理(第一章 数据库概述)
数据库·oracle
华为云开源1 小时前
openGemini 社区人才培养计划:助力成长,培养新一代云原生数据库人才
数据库·云原生·开源
GoppViper1 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理