--springboot配置文件中的数据库密码加密有什么好的方案?
有时开发一个项目或一个应用,不是直接开始就写代码就行了,同时还要考虑许多问题,特别是你的项目或应用投入正式生产运行时,安全因素是不得不面对,这就是架构师、项目经理、产品经理都要提前就考虑的问题,在安全需求侧,产品设计和架构设计就要考虑进来。
比如项目中一定少不连接数据库,如果开发人员按一般教程学的来开发,大概率是将数据库密码明文方式配置在配置文件中,这种方式在开发(DEV)状态下没什么问题,要是发布生产环境(PROD)系统那可是大忌,存在安全隐患。今天我用我超过10年的项目经验结合我的新助手chartGPT帮我整理的资料给大家说一下有几种解决方案。
“关注”“点赞”“收藏”=不迷路
往期更精彩
有以下几种方案可以实现在Spring Boot配置文件中加密数据库密码:
1.使用Jasypt进行加密
Jasypt是一个Java库,提供了基于密码学的简单加密和解密服务。我们可以使用Jasypt对Spring Boot配置文件中的敏感信息进行加密。
Step 1:添加Jasypt依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency> |
Step 2:在配置文件中添加加密配置
jasypt: encryptor: password: my-secret-key |
Step 3:对数据库密码进行加密
将密码使用以下格式加密并替换到配置文件中:
# ENC(加密后的密码) spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/XXXX username: dbuser password: ENC(MhJX7Oq5SN+0oAPfhSS0mOQpTi0PneHrgQloBEXh/XXX==) jasypt: encryptor: algorithm: PBEWITHHMACSHA512ANDAES_256 iv-generator-classname: org.jasypt.iv.RandomIvGenerator pool-size: 1 salt-generator-classname: org.jasypt.salt.RandomSaltGenerator password: my-secret-key |
在此之前可使用Jasypt-CLI命令行工具对密码进行加密,取得加密后的内容:
$ java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="my-password" password="my-secret-key" algorithm= PBEWITHHMACSHA512ANDAES_256 ----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.121-b14 ----ARGUMENTS------------------- input=my-password algorithm=PBEWithMD5AndDES password=my-secret-key ----OUTPUT---------------------- jKuUpRQBUMnx6gh0LXjvPQ1111== |
将输出的字符串 `jKuUpRQBUMnx6gh0LXjvPQ1111==` 替换配置文件中的数据库密码,加NEC()函数方式。
Step 4: 在代码中使用加密后的密码
在SpringBootApplication主类中加入@EnableEncryptableProperties注解。
在那在在代码中使用 `@Value` 注解获取加密后的密码,之后用Jasypt进行解密来使用。
@Slf4j @SpringBootApplication @EnableEncryptableProperties public class ExxxxxxStartApp { |
2. 对称加密
在配置文件中使用对称加密算法,将数据库密码加密。在代码中使用相应的密钥解密密码。
Step 1: 安装 JCA (Java Cryptography Extension) 扩展, 如 bouncycastle (bcprov-jdk15on-158.jar)
在 pom.xml 中添加依赖:
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.58</version> </dependency> |
Step 2: 在配置文件中添加加密后的密码
将加密后的密码替换到配置文件中:
spring: datasource: password: AES:aWQmbmJ0eSUzRA==:wYpkrWqrs0jZ7Fomfh6dHg== |
其中 `AES` 为加密算法,`aWQmbmJ0eSUzRA==` 为盐(salt),`wYpkrWqrs0jZ7Fomfh6dHg==` 为加密后的密码。
Step 3: 在代码中使用密钥解密密码
在代码中使用密钥解密密码:
import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusDBConfig { @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource() throws Exception { String key = "my-secret-key"; // 密钥 byte[] iv = new byte[16]; // 随机向量 IvParameterSpec ivspec = new IvParameterSpec(iv); byte[] decodedPwd = Base64.decodeBase64(password.split(":")[2]); // 解码后的密码 SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(decodedPwd); // 解密后的密码 DruidDataSource dataSource = new DruidDataSource(); dataSource.setPassword(new String(original)); //其他配置 return dataSource; } } |
3. 使用Spring Cloud Config Server
Spring Cloud Config Server是一个分布式配置中心,可以将配置信息集中管理,并提供REST API访问。可以将敏感信息存储在Config Server中,并使用加密机制保证安全。
Step 1: 启动Config Server
在 Config Server 项目的 `application.yml` 中添加以下配置:
encrypt: key: my-secret-key spring: application: name: order-config-server cloud: config: server: git: uri: https://github.com/my-org/my-config-repo.git username: my-username password: my-password |
其中 `encrypt.key` 为加密键,`spring.cloud.config.server.git.uri` 为存储配置信息的Git库地址。
- 加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> |
- 编写启动类:
@EnableConfigServer @SpringBootApplication(scanBasePackages = "com.github.mg0324") public class MyStartupApplication { public static void main(String[] args) { SpringApplication.run(MyStartupApplication.class,args); } } |
启动查看 http://127.0.0.1:8080/mic-test-dev.yaml
访问mic-test-dev.yaml会整合mic-test.yaml的内容。
Step 2: 将配置文件加密并存储在Git库中
使用 `curl` 命令将配置文件加密并提交到Git库:
$ curl -X POST --data-urlencode "plaintext=@application.yml" http://localhost:8888/encrypt -u user:usepassword my-encrypted-password |
其中 `user:password` 为Git库的用户名和密码。`my-encrypted-password` 为加密后的密码,将其替换到配置文件中。
Step 3: 在代码中使用加密后的密码
在代码中使用Spring Cloud的 `@Value` 和 `@RefreshScope` 注解获取加密后的密码:
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Configuration; @RefreshScope @Configuration public class MybatisPlusDBConfig { @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setPassword(password); //其他配置 return dataSource; } } |
也可以用Nacos配置中心,这个支持更多平台与开发语言。
Nacos可实现系统配置的集中管理(编辑、存储、分发)、动态更新不重启、回滚配置(变更管理、历史版本管理、变更审计)等所有与配置相关的活动。
希望大家能一如既往开心愉快!不管大环境如何,都能保持一颗积极乐观的心态,用心对待生活。
?往期内容:
本文暂时没有评论,来添加一个吧(●'◡'●)