spring boot对数据库连接信息加密与数据库内容加密

1.依赖

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

加密密钥
2_1直接写在yml(不安全)

jasypt:
  encryptor:
    password: ceshimiwen123123	#自己随便写

2_2作为启动参数

 -Djasypt.encryptor.password=ceshimiwen123123

在这里插入图片描述
java -jar xxx.jar -Djasypt.encryptor.password=ceshimiwen123123

3.写个测试类(启动测试类时密钥直接放yml里就行,拿到加密后信息再放启动参数)

package ceshi.test;

import org.jasypt.encryption.StringEncryptor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class JasyptTest {
    /**
     * 注入加密方法
     */
    @Autowired
    private StringEncryptor encryptor;
    /**
     * 手动生成密文,此处演示了url,user,password
     */
    @Test
    public void encrypt() {
        String url = encryptor.encrypt("jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        String name = encryptor.encrypt("root");
        String password = encryptor.encrypt("root");
        System.out.println("database url: " + url);
        System.out.println("database name: " + name);
        System.out.println("database password: " + password);
        Assert.assertTrue(url.length() > 0);
        Assert.assertTrue(name.length() > 0);
        Assert.assertTrue(password.length() > 0);
    }
}

拿到加密后字符串
在这里插入图片描述
以ENC()包裹,例如:在这里插入图片描述
启动成功
在这里插入图片描述
jasypt 默认使用 ENC() 包裹

修改前后缀

jasypt:
	encryptor:
		## 指定前缀、后缀
		property:
			prefix: 'PASS('
			suffix: ')'

此时使用PASS() 包裹才会被解密

数据库内容加密
1.数据库函数
简单方法直接用函数加密:to_base64(字符串),解密:from_base64(字符串),有中文则使用去除乱码CONVERT(from_base64(字符串),CHAR)

2.springboot自带md5加密
import org.springframework.util.DigestUtils;
String md5Password = DigestUtils.md5DigestAsHex(字符串.getBytes());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

翎墨袅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值