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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chyanwu68/article/details/102614551

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);
    }

4)将上面的生成的密匙如下替换,此处主要是数据库密码
密文使用ENC进行标识{ENC( )是固定写法,( )里面是加密后的信息。}

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

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);
    }

注意:替换完密文的密码后,再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 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值