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

发布于:2025-02-11 ⋅ 阅读:(98) ⋅ 点赞:(0)

🍂 写在前面

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

// 明文形式
spring.datasource.mysql.jdbc-url=jdbc:mysql://127.0.0.1:3306/transfer123456?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

🌰 步骤

⚠️ 第一步 添加引入依赖

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

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

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

jasypt.encryptor.password=私钥字符串

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

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=datamysql algorithm=PBEWithMD5AndDES

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

password 就是你的私钥字符串

algorithm=PBEWithMD5AndDES 固定的不需要改

🚒 解密命令 (一般不需要)

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=datamysql algorithm=PBEWithMD5AndDES

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

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



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

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


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

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


The end


网站公告

今日签到

点亮在社区的每一天
去签到