在做项目的时候,大多数情况,我们会将连接的数据库账号和密码写在yml配置文件中,不过这种方式很可能会导致安全问题(密码泄露),所以今天跟大家分享一下SpringBoot数据库加密操作
由于是gradle项目,所以导入依赖的方式与maven有所区别,要在build.gradle中导入依赖
compile group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.4'
复制
这里我使用的是最新版本3.0.4
导入依赖后,就可以写一个测试类来通过算法进行加密,代码如下:
package com.taikang.ptms;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;
//public class JasyptTest {
// public static void main(String[] args) {
// BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
// 加密所需的salt(盐),自定义
// textEncryptor.setPassword("retail_salt");
// 要加密的数据(数据库的用户名或密码)
// String username = textEncryptor.encrypt("retail_u");
// String password = textEncryptor.encrypt("retail_PWD_123");
// System.out.println("username:"+username);
// System.out.println("password:"+password);
// }
//}
public class JasyptTest {
// 加密
@Test
public static void testEncrypt() throws Exception {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
// 加密的算法,这个算法是默认的
config.setAlgorithm("PBEWithMD5AndDES");
// 加密的密钥
config.setPassword("minjd");
standardPBEStringEncryptor.setConfig(config);
String plainText = "eUU*RgTCD1dHiDFv";
String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
System.out.println(encryptedText);
// String encryptedText = "BkXvO0ayR2Gg1bMR0rh7qKePSzfcxOBjuVdpD6wOX+I1ESrb1Ldbwo+C48lb3ENN";
// String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
// System.err.println(plainText);
}
// 解密
@Test
public static void testDe() throws Exception {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
// 加密的算法,这个算法是默认的
config.setAlgorithm("PBEWithMD5AndDES");
// 加密的密钥
config.setPassword("minjd");
standardPBEStringEncryptor.setConfig(config);
String encryptedText = "BkXvO0ayR2Gg1bMR0rh7qKePSzfcxOBjuVdpD6wOX+I1ESrb1Ldbwo+C48lb3ENN";
String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
System.err.println(plainText);
}
public static void main (String[] args) throws Exception{
testEncrypt();
// testDe();
}
}
复制
其中使用的算法是默认的MD5加密算法,plaintext中写的是要加密的数据库密码,而password则为自定义的salt值,相当于密钥,上述代码中包含了加密操作和解密操作,写好后首先可以在本地运行一下,获得加密密码
获得加密密码后,我们可以将本地local.yml中的数据库密码改为ENC(加密后密码)的格式,同时在yml配置文件中上下面这段代码:
###jasypt加密的盐值###
jasypt:
encryptor:
iv-generator-classname: org.jasypt.iv.NoIvGenerator
algorithm: PBEWithMD5AndDES
password: minjd
复制
在运行前还需要在Run-edit configration中编辑一下VM,如下:
-Djasypt.encryptor.password=minjd
复制
java -jar -Djasypt.encryptor.password=minjd
复制
文章转载自琢磨先生DataBase,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
一种极简单的SpringBoot单元测试方法
京东云开发者
36次阅读
2025-03-11 15:50:25
缓存监控治理在游戏业务的实践和探索
vivo互联网技术
35次阅读
2025-03-20 09:51:10
Worker模块源码实战:万字长文解析DolphinScheduler如何实现亿级任务调度
海豚调度
31次阅读
2025-03-04 09:47:24
面对.restbackup勒索病毒,你的数据安全何去何从?
weixin_shujuxf
29次阅读
2025-03-06 20:46:51
Java的SPI机制详解
京东云开发者
27次阅读
2025-03-05 11:47:12
数仓建模:基于OTD流程的订单履约分析
会飞的一十六
27次阅读
2025-03-03 09:53:12
.kat6.l6st6r勒索病毒来袭:数据文件安全与恢复之道
91huifu.com
26次阅读
2025-03-21 21:15:02
.wex勒索病毒”全解析:了解这一网络威胁
91huifu.com
26次阅读
2025-03-15 19:27:18
C#操作SQLite数据库
淡定
24次阅读
2025-03-25 23:27:25
.hero77勒索病毒:网络安全领域的新挑战与应对策略
weixin_shujuxf
19次阅读
2025-03-22 19:10:14