惠州市网站开发,对象存储oss做视频网站,新浪军事 手机新浪网,商业设计网1、需求
在使用SpringBoot开发过程中#xff0c;会将一些敏感信息配置到SpringBoot项目的配置文件中(不考虑使用配置中心的情况 )#xff0c;例如数据库的用户名和密码、Redis的密码等。为了保证敏感信息的安全#xff0c;我们需要将此类数据进行加密配置。
2、操作步骤
…1、需求
在使用SpringBoot开发过程中会将一些敏感信息配置到SpringBoot项目的配置文件中(不考虑使用配置中心的情况 )例如数据库的用户名和密码、Redis的密码等。为了保证敏感信息的安全我们需要将此类数据进行加密配置。
2、操作步骤
2.1 添加依赖
目前通用的做法是使用 jasypt 对数据库用户名或者密码进行加密在springboot项目的POM中添加如下依赖目前最新的版本是3.0.3但是我们不用最新版本而是使用2.1.2版本。后边会说明原因。
!--数据库密码加密依赖--
dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversion2.1.2/version
/dependency2.2 生成密文
依赖添加之后会在你本地的maven仓库中下载相关的依赖包进入到你自己的maven仓库通过路径/org/jasypt/jasypt/1.9.3,找到 jasypt-1.9.3包。
Windows系统 直接在文件地址栏输入cmd进入到命令行。Mac系统通过Terminal终端进入到对应路径即可。
如下命令
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI inputroot passwordhelloWorld algorithmPBEWithMD5AndDESinput输入内容此处指的是数据库密码。password不是指的密码而是加密所使用的“盐”可以随便定义。algorithm加密所使用的算法非必填此处使用“PBEWithMD5AndDES” 算法。
如果想了解其他配置可以查看如下配置类
com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties执行命令显示如下信息
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI inputroot passwordhelloWorld algorithmPBEWithMD5AndDES----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”再执行一次操作。
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input123 passwordhelloWorld algorithmPBEWithMD5AndDES----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 数据库配置
生成加密密文后修改项目数据库连接的用户名和密码
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/test?useSSLfalseuseUnicodetruecharacterEncodingutf8allowMultiQueriestrueautoReconnecttrueusername: ENC(1vAgkftBPnIYh/gjCokbFA)password: ENC(LPbn37xuIIAfCkaermp5cQ)此时已经配置完成。
2.4 SpringBoot项目配置文件中配置“加密盐”
为了能够使程序获取我们加密前的数据需要在项目配置文件中配置“加密盐”即2.2节中所说的“password”如下所示
jasypt:encryptor:password: helloWorld但是在配置文件中配置加密盐也是不安全的如果别人知道了加密后的密文又知道了加密盐就可以通过如下解密命令进行解密这样就会导致我们的密码泄露。
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI inputLPbn37xuIIAfCkaermp5cQ passwordhelloWorld algorithmPBEWithMD5AndDES----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版本时按照以上配置完成后启动项目会出新如下报错信息
***************************
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.StringAction:Update your applications configurationDisconnected from the target VM, address: 127.0.0.1:56043, transport: socket较新的版本更改了相关算法会出现如上错误降低版本即可。
注意 如果使用了2.1.2版本但是没有配置 加密盐也会报上边的错误按照如上配置加密盐即可。