SpringBoot配置文件加密

SpringBoot配置文件加密

  1. 新建SpringBoot项目

  2. 在maven中引入jasypt加密组件

    <!-- jasypt加密组件: https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
            <dependency>
                <groupId>com.github.ulisesbocchio</groupId>
                <artifactId>jasypt-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
  3. 在配置文件中配置加密密钥

    #加密的密钥
    jasypt.encryptor.password=sc
  4. 新建测试类

    @Test
        public void sc(){
            // 对应配置文件中配置的加密密钥
            System.setProperty("jasypt.encryptor.password", "sc");
            StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());
            System.out.println("加密: " + stringEncryptor.encrypt("root"));
            System.out.println("加密: "+ stringEncryptor.encrypt("root"));
            System.out.println("解密: " + stringEncryptor.decrypt("itJ8zZfWj54ZIf1lLVojrg=="));
        }
  5. 将加密后的账号和密码复制到配置文件中,jasypt默认使用ENC()来标识加密,加载配置的时候检测到ENC()即会自动解密

    # 数据库连接地址
    spring.datasource.url=jdbc:mysql://localhost:3306/ceshi?serverTimezone=UTC
    # 数据库用户名&密码:
    spring.datasource.username=ENC(b6zfPR9R9aaTtl5VPwrvww==)
    spring.datasource.password=ENC(AvEzdYaTFniscWGhUw6M/ERwKwhpbZE7)

    jasypt默认使用ENC()来标识加密

    自定义加密标识CESHI()

    #加密前缀
    #jasypt.encryptor.property.prefix=CESHI(
    #加密后缀缀
    #jasypt.encryptor.property.suffix=)

以上我们的密钥也是保存在配置文件中的,一旦密钥泄露,信息被解密,安全隐患依然存在!因此我们可以通过将密钥设置为程序启动时的参数来避免。

java -Djasypt.encryptor.password=加密的密钥 -jar test.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值