数据库账号密码加密


前言

数据库账号密码加密


提示:以下是本篇文章正文内容

一、引入依赖

1.引入库

代码如下(示例):

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

2.读入数据

代码如下(示例):
提示:encryptor.setPassword()自己设置 password:盐值

public static void main(String[] args) {
        String account = "root";
        String password = "123";
        BasicTextEncryptor encryptor = new BasicTextEncryptor();

        //秘钥
        //encryptor.setPassword(System.getProperty("jasypt.encryptor.password"));
        encryptor.setPassword("eug83f3gG");
        //密码进行加密
        String newAccount = encryptor.encrypt(account);
        String newPassword = encryptor.encrypt(password);
        System.out.println("加密后账号:" + newAccount);
        System.out.println("加密后密码:" + newPassword);

        AnsiOutput.setEnabled(Enabled.ALWAYS);
        SpringApplication.run(PmsDcApplication.class, args);
    }

3.配置文件

3.1 数据库配置

提示:配置文件中数据库加密后的账号密码使用ENC()包裹

  datasource:
    MyDb1:
      driver-class-name: org.postgresql.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:postgresql://127.0.0.1:5432/test?reWriteBatchedInserts=true&currentSchema=test
      username: ENC(rtEkW3k0lptWxIbri8BvNg==)
      password: ENC(cSsZgL6Tx1egac46esgzGg==)

4 启动

4.1 (方式一)配置文件中配置盐值等(不建议,会暴漏)

提示:password使用自己设置的 algorithm:加密方式

jasypt:
  encryptor:
    password: eug83f3gG
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

4.2 (方式二)启动参数启动(建议使用)

4.2.1 jar包启动

提示:password使用自己设置的

jara -Djasypt.encryptor.password=eug83f3gG -Djasypt.encryptor.algorithm=PBEWithMD5AndDES -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator -jar wstest.1.1.1.jar
4.2.2 idea启动

启动参数:-Djasypt.encryptor.password=eug83f3gG -Djasypt.encryptor.algorithm=PBEWithMD5AndDES -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值