java全家桶之18: 使用 Baomidou 的 Spring DataSource 加密配置
Baomidou (MyBatis-Plus) 提供了数据源加密功能,可以保护数据库连接信息的安全性。
·
摘要:
Baomidou提供了Spring数据源加密方案,使用AES加密保护数据库连接信息。可通过@Bean配置加密数据源或在application.yml中使用"mpw:"前缀的加密配置,并配合MybatisPlusPropertiesCustomizer进行解密。建议将密钥存储在环境变量中,定期轮换密钥,并与Apollo等配置中心结合使用。该方案有效提升了数据库连接信息的安全性,防止敏感信息泄露。
使用 Baomidou 的 Spring DataSource 加密配置
Baomidou (MyBatis-Plus) 提供了数据源加密功能,可以保护数据库连接信息的安全性。以下是实现 Spring DataSource 加密的完整方案:
1. 添加依赖
首先确保项目中包含 MyBatis-Plus 和加解密工具依赖:
xml
运行
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> <!-- 或者单独使用加密工具 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>最新版本</version> </dependency>
2. 配置加密数据源
方法一:使用 @Bean
配置加密数据源
java
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties; import com.baomidou.mybatisplus.core.toolkit.AES; import com.zaxxer.hikari.HikariDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class EncryptedDataSourceConfig { // 加密密钥(16位) private static final String SECRET_KEY = "1234567890123456"; @Bean public HikariDataSource dataSource() { // 加密的数据库连接信息 String encryptedUrl = AES.encrypt("jdbc:mysql://localhost:3306/test", SECRET_KEY); String encryptedUsername = AES.encrypt("root", SECRET_KEY); String encryptedPassword = AES.encrypt("123456", SECRET_KEY); HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl(AES.decrypt(encryptedUrl, SECRET_KEY)); dataSource.setUsername(AES.decrypt(encryptedUsername, SECRET_KEY)); dataSource.setPassword(AES.decrypt(encryptedPassword, SECRET_KEY)); return dataSource; } }
方法二:在 application.yml 中使用加密配置
yaml
spring: datasource: url: mpw:qS4JTsJjJm0ePJq4JTsJjJm0ePJq4JTsJjJm0ePJq4 # 加密后的URL username: mpw:qS4JTsJjJm0ePJq4 # 加密后的用户名 password: mpw:qS4JTsJjJm0ePJq4 # 加密后的密码 hikari: pool-name: HikariCP maximum-pool-size: 20
3. 加解密工具使用
加密数据
java
import com.baomidou.mybatisplus.core.toolkit.AES; public class EncryptUtil { public static void main(String[] args) { String secretKey = "1234567890123456"; // 16位密钥 String originalUrl = "jdbc:mysql://localhost:3306/test"; // 加密 String encryptedUrl = AES.encrypt(originalUrl, secretKey); System.out.println("加密后的URL: " + encryptedUrl); // 解密 String decryptedUrl = AES.decrypt(encryptedUrl, secretKey); System.out.println("解密后的URL: " + decryptedUrl); } }
配置解密处理器
java
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer; import com.baomidou.mybatisplus.core.toolkit.AES; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { private static final String SECRET_KEY = "1234567890123456"; @Bean public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() { return properties -> { // 解密URL if (properties.getDataSource().getUrl().startsWith("mpw:")) { String encryptedUrl = properties.getDataSource().getUrl().substring(4); properties.getDataSource().setUrl(AES.decrypt(encryptedUrl, SECRET_KEY)); } // 解密用户名 if (properties.getDataSource().getUsername().startsWith("mpw:")) { String encryptedUsername = properties.getDataSource().getUsername().substring(4); properties.getDataSource().setUsername(AES.decrypt(encryptedUsername, SECRET_KEY)); } // 解密密码 if (properties.getDataSource().getPassword().startsWith("mpw:")) { String encryptedPassword = properties.getDataSource().getPassword().substring(4); properties.getDataSource().setPassword(AES.decrypt(encryptedPassword, SECRET_KEY)); } }; } }
4. 安全建议
-
密钥管理:
-
不要将密钥硬编码在代码中
-
推荐使用环境变量或配置中心存储密钥
-
生产环境使用不同的密钥
-
-
加密内容:
-
至少加密用户名和密码
-
建议也加密JDBC URL中的敏感信息
-
-
密钥轮换:
-
定期更换加密密钥
-
更换密钥时需要重新加密所有配置
-
5. 与Apollo配置中心结合
如果使用Apollo配置中心,可以这样配置:
java
复制
下载
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import com.ctrip.framework.apollo.Config; import com.baomidou.mybatisplus.core.toolkit.AES; @Configuration public class ApolloDataSourceConfig { @ApolloConfig private Config config; private static final String SECRET_KEY = System.getenv("DB_ENCRYPT_KEY"); @Bean public DataSource dataSource() { HikariDataSource dataSource = new HikariDataSource(); // 从Apollo获取加密配置 String encryptedUrl = config.getProperty("spring.datasource.url", ""); String encryptedUsername = config.getProperty("spring.datasource.username", ""); String encryptedPassword = config.getProperty("spring.datasource.password", ""); // 解密配置 dataSource.setJdbcUrl(AES.decrypt(encryptedUrl, SECRET_KEY)); dataSource.setUsername(AES.decrypt(encryptedUsername, SECRET_KEY)); dataSource.setPassword(AES.decrypt(encryptedPassword, SECRET_KEY)); return dataSource; } }
这样既利用了Apollo的配置中心能力,又通过Baomidou的加密工具保护了敏感数据。
更多推荐
所有评论(0)