◉◡◉ 您好,欢迎到访伊成个人站!

springboot项目对数据库密码加解密

写在前面

我相信大多数人在配置文件中都是以明文的形式配置数据库的用户名和密码的,如下展示:

1
2
3
4
5
// 明文形式
spring.datasource.mysql.jdbc-url=jdbc:mysql://127.0.0.1:3306/data?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.mysql.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.mysql.username=root
spring.datasource.mysql.password=123456

显然这样配置不是很安全,所以我们可以对用户名和密码进行加密,这样配置后需要进行解密才知道我们配置的用户名和密码具体是什么。

接下来跟着步骤 do it

步骤

第一步 添加引入依赖

1
2
3
4
5
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

第二步 制定私钥和生成对应的加密串

在你的配置文件中配置好你的私钥字符串

1
jasypt.encryptor.password=私钥字符串

第一步完成后找到对应依赖文件夹里面 org\jasypt\jasypt\1.9.2>
进行加密操作

1
2
3
C:\Users\usmart\.m2\repository\org\jasypt\jasypt\1.9.2>
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI
input="root" password=data algorithm=PBEWithMD5AndDES

input 里面就是你要加密的字符串

password 就是你的私钥字符串

algorithm=PBEWithMD5AndDES 固定的不需要改

解密命令 (一般不需要)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
C:\Users\usmart\.m2\repository\org\jasypt\jasypt\1.9.2>java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="ri60tsMfnqk+usK+nXjQuQ==" password=data algorithm=PBEWithMD5AndDES

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

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



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

algorithm: PBEWithMD5AndDES
input: ri60tsMfnqk+usK+nXjQuQ==
password: data


----OUTPUT----------------------
root

到这里就结束了,两步就搞定了。


The end

支付宝打赏 微信打赏