springboot整合jasypt 加密配置文件

系列文章目录


一、先看一份典型的配置文件

... 省略 ...

## 配置MySQL数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

## 配置Redis缓存连接
redis.host=localhost
redis.port=6379
redis.password=111111

## 配置SMS短信服务连接
ali.sms.access_key_id=qweqdadasd
ali.sms.access_key_secret=bs4ImWdvss6iy0him8ly

... 省略 ...

这乍一看没啥问题,但是密码都是明文其实很危险

二、哪些信息要加密呢?

一般来说,项目配置文件里,所有涉及信息安全的配置项(或字段)都应该做处理,典型的比如:

  • 用到的数据库、缓存的密码
  • 用到的中间件、消息队列的密码
  • 用到的各种第三方服务的Access_Key
  • 其他第三方服务的通信信息
  • …等等

三、如何加密配置项呢?

3.1、使用 jasypt-spring-boot 加密

引入依赖

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

3.2、在配置文件中指定自定义加密器的bean

指定完,就创建这bean

# 指定bean
jasypt.encryptor.bean=CodeEncrypBean

3.2、使用自定义加密器(创建bean)

说明:

使用自定义加密器,这样会更安全,因为如果将加密密钥写在配置文件中,这跟没有加密差不多。

jasypt.encryptor.password=abc(你的密钥)

定义个配置类 MyJasyptConfig.java

package sqy.db_safe.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author suqinyi
 * @Date 2021/7/9
 */
@Configuration
public class MyJasyptConfig {

    private String key = "sqy_safe";

    @Bean(name = "CodeEncrypBean")
    public StringEncryptor CodeEncrypBean() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();

        SimpleStringPBEConfig config = new SimpleStringPBEConfig();

        config.setPassword(key);
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);

        return encryptor;
    }

    //测试
    public static void main(String[] args) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("sqy_safe");
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        //===================================
        String encrypt = encryptor.encrypt("root");
        String decrypt = encryptor.decrypt("ZxY08m8wOk4qE/cTEgfzhRbYQlxKg5mhG+kZ6P5lc0MQwy87Z3MouPFWyVGlGyPf");
        System.out.println(encrypt);
        System.out.println(decrypt);

    }
}


运行下测试得到加密后的密码

明文密码是root
密文就是那一大串
在这里插入图片描述

3.4、在配置文件中将密码替换

注意:jasypt人家默认是要这样识别 ENC(密文)

例如:

# 没有加密的明文密码是root
# spring.datasource.password=root

# 加密后的root密码---ENC()包裹起来
spring.datasource.password=ENC(8rXZXNjNt1kKEQS+iKyJEqwo740LWB0wia3I5J/PRwXNQl0Eor4oWFVhy28iZ39j)

3.4、不使用NNC()包裹,自定义

自己定义前后缀

  1. jasypt.encryptor.property.prefix=
  2. jasypt.encryptor.property.suffix=

例如

# 加密配置
    # 用sql()包裹密文
jasypt.encryptor.property.prefix=sqy(
jasypt.encryptor.property.suffix=)

#使用sql()包裹
spring.datasource.password=sqy(8rXZXNjNt1kKEQS+iKyJEqwo740LWB0wia3I5J/PRwXNQl0Eor4oWFVhy28iZ39j)

3.5、完整的application.properties

# 应用名称
spring.application.name=db_safe
# 应用服务 WEB 访问端口
server.port=7894

# 配置MySQL数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test01?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
#spring.datasource.password=root
spring.datasource.password=sqy(8rXZXNjNt1kKEQS+iKyJEqwo740LWB0wia3I5J/PRwXNQl0Eor4oWFVhy28iZ39j)

# druid数据库连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# mybatis指定xml位置
mybatis.mapper-locations=classpath:mapper/**/*Mapper.xml

# 加密配置
    # 用sql()包裹密文
jasypt.encryptor.property.prefix=sqy(
jasypt.encryptor.property.suffix=)
# 自定的配置安全配置类
jasypt.encryptor.bean=CodeEncrypBean


当然这个加密的密钥还有其他的引入方式,但是小编感觉这种就很可以了

方式一:直接作为程序启动时的命令行参数来带入
方式二:直接作为程序启动时的应用环境变量来带入
方式三:甚至可以作为系统环境变量的方式来带入–最安全

四、完结

这个案例可以在小编的这篇springboot整合mybatis上面进行添加测试

地址 ==> sprongboot整合mybatis

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Jasypt(Java Simplified Encryption)是一个Java加密库,它可以用来加密/解密文本、哈希密码等。Spring Boot提供了对Jasypt的支持,可以在应用程序中轻松使用Jasypt加密敏感信息。下面是使用Jasypt对MySQL配置文件进行加密的步骤: 1. 引入Jasypt依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置加密 在application.properties或者application.yml中添加以下配置: ```properties jasypt.encryptor.password=your_password ``` 其中,your_password是用来加密敏感信息的密码。 3. 加密MySQL配置信息 在application.properties或者application.yml中加入MySQL配置信息,如下: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root ``` 将密码部分使用Jasypt进行加密,如下: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=ENC(加密后的密码) ``` 其中,加密后的密码可以使用Jasypt提供的加密工具进行加密。例如,我们可以使用以下命令生成加密后的密码: ``` java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password="your_password" algorithm=PBEWithMD5AndDES ``` 其中,jasypt-1.9.2.jar是Jasypt的jar包,your_password是用来加密敏感信息的密码。执行以上命令后,会输出加密后的密码。 4. 配置解密 在启动应用程序时,Spring Boot会自动解密加密的敏感信息。因此,我们不需要任何额外的配置来解密MySQL密码。只需将加密后的密码放入配置文件即可。 至此,我们已经成功地使用Jasypt对MySQL配置文件进行了加密

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suqinyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值