IDEA中SpringBoot项目数据库连接加密方法

Source

1. maven添加相应版本的依赖

这里注意,不能使用太新的版本,本人开发环境使用3.0.3版本时,报以下错误,使用2.1.2时报错消失。

***************************
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. 更新maven依赖

更新后,到仓库中找到加密工具jasypt-1.9.3.jar

3. 使用jasypt工具生成密文

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=待加密字符串 password=盐 algorithm=PBEWithMD5AndDES

4. 修改application-dev.yml文件中待加密的数据源密码

ENC(FtvDryJcYcWdFNpfxUs8Sw==)

5. 编辑配置,增加项目启动参数

-Djasypt.encryptor.password=1234

或者 application.yml 文件中增加jasypt的盐配置项:

jasypt:
  encryptor:
    password: 1234

6. 启动调试项目

可以看到日志中,数据库连接成功,项目启动成功。