软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot配置文件中数据库密码加密两种方案(推荐)

SpringBoot配置文件中数据库密码加密两种方案(推荐)

作者:伍婷  发布时间:2023-03-16 22:40:55 

标签:spring,boot,配置文件,加密

SpringBoot项目经常将连接数据库的密码明文放在配置文件里,安全性就比较低一些,尤其在一些企业对安全性要求很高,因此我们就考虑如何对密码进行加密。

介绍两种加密方式:jasypt 可加密配置文件中所有属性值; druid 自带了加解密,可对数据库密码进行加密。

jasypt 加解密

jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中。可以快速集成到 Spring Boot 项目中,并提供了自动配置,使用非常简单。

步骤如下:

1)引入maven依赖


<dependency>
 <groupId>com.github.ulisesbocchio</groupId>
 <artifactId>jasypt-spring-boot-starter</artifactId>
 <version>2.1.1</version>
</dependency>

2)在配置文件application


#jasypt加密的盐值
jasypt.encryptor.password=erp

3)测试用例中生成加密后的密匙


@Autowired
 StringEncryptor encryptor;

@Test
 public void getPass() {
   String url = encryptor.encrypt("jdbc:mysql://127.0.0.1:3306/erp-framework?useUnicode=true&characterEncoding=utf8");
   String name = encryptor.encrypt("root");
   String password = encryptor.encrypt("mysql");
   System.out.println(url+"----------------");
   System.out.println(name+"----------------");
   System.out.println(password+"----------------");
   Assert.assertTrue(name.length() > 0);
   Assert.assertTrue(password.length() > 0);
 }

执行后,会生成:

BA9uZ35umFic6NbuaLdGzZBodw/wSqvztMt9UGdlOtxxs/fr/W5kf8Bs6GzzHklNfkcU30g8aQ/XdihsZtqRz1J34zNIQxuH3BCG1kknFayp13G8RhkeF4ptBfx6i6nqnP4Uc0UKpjcsxxfTZImHBVvcTY0RDANk26IGBPZvQry7qKuna/RTMQ==
kyMvAncHqzcvGAildsK67w==
7QCSL5/HKjxFQRPLGgGH7kAElrmf/mgQ

4)将上面的生成的密匙如下替换,此处主要是数据库密码密文使用ENC进行标识


first.spring.datasource.password=ENC(7QCSL5/HKjxFQRPLGgGH7kAElrmf/mgQ)

ENC( )是固定写法,( )里面是加密后的信息。

druid 非对称加密

数据库连接池 Druid 自身支持对数据库密码的加密解密,具体可以看git:https://github.com/alibaba/druid/wiki/%E5%A6%82%E4%BD%95%E5%9C%A8Spring-Boot%E4%B8%AD%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86%EF%BC%9F

1)引入maven依赖


<!-- druid -->
<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>druid</artifactId>
 <version>1.1.12</version>
</dependency>

2)在配置文件application


first.spring.datasource.username = root
first.spring.datasource.password = iujrgO5nD09lQfS9+SbTvOf4Wcp8jtJOSDnqSEq1jnFEUYW3UE/2bMMLv/Y74wSXUPUu6H6L/SJWkYpYujZ9AA==
first.spring.datasource.publicKey = MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJHmM9MkpAGxAnx/6+b2zNUIXYQUpgIOQBbEzkBmhgrrOa2oxYNzm7zYGuBlAugYzk1zSW8SAVT/b9ihGvHqutsCAwEAAQ

其中first.spring.datasource.password为第三步中生成的

3)测试用例中生成加密后的密匙


@Test
 public void druidEncrypt() throws Exception {
   //密码明文
   String password = "mysql";
   System.out.println("明文密码: " + password);
   String[] keyPair = ConfigTools.genKeyPair(512);
   //私钥
   String privateKey = keyPair[0];
   //公钥
   String publicKey = keyPair[1];

//用私钥加密后的密文
   password = ConfigTools.encrypt(privateKey, password);

System.out.println("privateKey:" + privateKey);
   System.out.println("publicKey:" + publicKey);

System.out.println("password:" + password);

String decryptPassword = ConfigTools.decrypt(publicKey, password);
   System.out.println("解密后:" + decryptPassword);
 }

执行后,会生成:

明文密码: mysql
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAglWrmLR1m+uUmfrscYuHda/iWFOfCcXv8DswDEvkvAz62dE3iJGJuhDa38LlbD+ps9kvLfPzaKcCiXWq2+ICzwIDAQABAkBA+rlcnsvr+p3RzxpG2culTxVH+BjYZIjlenzQBJ57aj8aylcm1x56usRNV1NGWeBJhAzJyXXi+v72goI3aqeRAiEAt0KGJMDvpcwM3aGUPIcFDa0YaU9KoqqwnwYUd7d5YSMCIQC2EUnF8NecvgpDycXqvjurh1qKLyfuYNg9fHEpaWmQZQIhAKtE99tnmVEJR8jmVoTO5zEl4ZeiLC2kepbUdtJ/3WejAiEAlAk0idxNA+ZFZF7gLQEmdU6l4Gt9NUbLLBtNy5UpPz0CIDS9ifH9wHONgLDEWh2w0lmxHni/LCQFbFunUzyAJTRZ
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIJVq5i0dZvrlJn67HGLh3Wv4lhTnwnF7/A7MAxL5LwM+tnRN4iRiboQ2t/C5Ww/qbPZLy3z82inAol1qtviAs8CAwEAAQ==
password:H8wvdSyjc9gAsdlbJ23G6kkgp4dEAwGRd6DABCftxBznVag5t+m/XWaRgtuznX0e5uXH2wxupeLiOUojo20IVg==
解密后:mysql

注意:替换完密文的密码后,再application.properties中还得配置:


# 配置监控统计拦截的filters
first.spring.datasource.filters=stat,log4j2,config
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
first.spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${first.spring.datasource.publicKey}

本文中对application.properties中的属性都是自定义的,会在项目启动的时候,通过@Configuration来加载,

完整的代码可以从git上获取:https://github.com/chyanwu/erp-framework

总结

以上所述是小编给大家介绍的SpringBoot配置文件中数据库密码加密两种方案,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://blog.csdn.net/chyanwu68/article/details/102614551

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com