druid 连接池RSA加密解密,实现yml配置文件中数据库连接信息如用户名,密码等信息加密解密
druid
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
项目地址:https://gitcode.com/gh_mirrors/druid/druid
免费下载资源
·
1 pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
2 写个工具类,用于生成公钥私钥,对数据进行加密解密等
import com.alibaba.druid.filter.config.ConfigTools;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
public class DruidEncryptUtil {
private static String publicKey;
private static String privateKey;
static {
try {
String[] keyPair = ConfigTools.genKeyPair(512);
privateKey = keyPair[0];
System.out.println("私钥:" + privateKey);
System.out.println();
publicKey = keyPair[1];
System.out.println("公钥:" + publicKey);
System.out.println();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
public static String encrypt(String plainText) throws Exception {
String encrypt = ConfigTools.encrypt(privateKey, plainText);
return encrypt;
}
public static String decrypt(String encryptText) throws Exception {
String decrypt = ConfigTools.decrypt(publicKey, encryptText);
return decrypt;
}
public static void main(String[] args) throws Exception {
String encrypt = encrypt("123456");
System.out.println("原文数据加密后:" + encrypt);
System.out.println();
String decrypt = decrypt(encrypt);
System.out.println("解密后的原文:" + decrypt);
}
}
3 修改yml文件,先用上面的工具类,把需要的数据加密,并复制公钥到yml中,关注注释:改动
spring:
application:
name: demo-shop
datasource:
url: jdbc:mysql://localhost:3306/mydb-1?useUnicode=true&characterEncoding=utf-8
username: root
# 改动1
password: byOoGNulf855VLIb5RoFJHGF2ICpNMBXqmUDKMOXr8AZSqgVALhAo9SPdNjaSAFLKq0XIRGOtDkt9B48H2t1FQ==
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
druid:
# 改动2
connectionProperties: config.decrypt=true;config.decrypt.key=${publicKey};
# 改动3,启用ConfigFilter
filter:
config:
enabled: true
# 改动4
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJCA44FIppHTdSwaxY5NYWJDpTbXhBK1fWIsRTzWgQuJO4O5HKvyJpWwHP9luMf4b2WeGDuvtWuC1+FxVY94tNECAwEAAQ==
GitHub 加速计划 / druid / druid
27.83 K
8.56 K
下载
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:3 个月前 )
f060c270 - 6 天前
1613a765
* Improve gaussdb ddl parser
* fix temp table 7 天前
更多推荐
已为社区贡献11条内容
所有评论(0)