网络编程
位置:首页>> 网络编程>> 数据库>> jasypt 集成SpringBoot 数据库密码加密操作

jasypt 集成SpringBoot 数据库密码加密操作

作者:夏日鸢尾  发布时间:2024-01-26 03:34:48 

标签:jasypt,SpringBoot,数据库,加密

昨天看到一片文章,说是某某旗下酒店数据库因为程序员不小心,把数据库明文密码上传到了GitHub上,导致酒店数据注册资料、入住信息,开房记录被下载倒卖的消息。

作为程序员,开发的时候为了简单,账户明明都设置很简单,基本上数据库密码都是明文的,没做什么操作,至少我待过的公司都是这样,无论是测试环境还是线上环境,想想,这个也是一大安全隐患,在此,趁现在不忙,做些基于springboot的数据库密码加密。

1、pom.xml添加jar包(不同jdk选择不同的版本):


<!-- jdk8 版本 整合jasypt对数据库密码加密 -->
<dependency>
 <groupId>com.github.ulisesbocchio</groupId>
 <artifactId>jasypt-spring-boot-starter</artifactId>
 <version>1.14</version>
</dependency>
<!-- jdk7版本-->
<dependency>
 <groupId>com.github.ulisesbocchio</groupId>
 <artifactId>jasypt-spring-boot-starter</artifactId>
  <version>1.5-java7</version>
</dependency>
<!-- jdk6版本-->
<dependency>
 <groupId>com.github.ulisesbocchio</groupId>
 <artifactId>jasypt-spring-boot-starter</artifactId>
  <version>1.5-java6</version>
</dependency>

2、window 窗口打开命令窗口,输入命令:

java -cp D:\mavenspace\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=allanpassword algorithm=PBEWithMD5AndDES

java –cp jar包所在路径\jar包 org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=”你的数据库密码” password=加密字段,随意设置algorithm=默认PBEWithMD5AndDES加密

参数说明:

input =数据库链接密码

password=加密字段,随意设置(配置文件中需要添加此密码,相当于约定密码)

algorithm= 算法,默认PBEWithMD5AndDES

运行命令后如图:

jasypt 集成SpringBoot 数据库密码加密操作

----OUTPUT----------------------

Ekgkm+TzSmf7w7bcr90gMV6MAwj0waW+

OUTPUT 就是加密后的密码,在配置项目密码时结合ENC() 使用,如下图:

3、项目中添加配置(以springboot为例),如图:

jasypt 集成SpringBoot 数据库密码加密操作

# 数据库密码加密配置


jasypt:
encryptor:
password: allanpassword
spring MySQL密码写法:
password: ENC(Ekgkm+TzSmf7w7bcr90gMV6MAwj0waW+)

4、springboot启动类添加注解使其生效:

jasypt 集成SpringBoot 数据库密码加密操作

以上配置完成,启动项目运行,查询数据库,成功!

补充知识:SpringBoot(Spring)使用jasypt处理加密问题

前景:为了防止配置文件里面的明文密码泄露

1.引入依赖:(针对SpringBoot的)


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

2.配置文件配置参数

#这里可以理解成是加解密的时候使用的密钥 (也可以考虑多配置认证信息jasypt.encryptor.algorithm)

jasypt.encryptor.password=youPassword

3.编写测试类得到加密密码


@Autowired
StringEncryptor stringEncryptor;

@Test
public void encryptPwd() {
 String result = stringEncryptor.encrypt("yourPassword");
 System.out.println(result);  
}

4.修改配置文件里面的明文密码


spring.datasource.druid.first.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.first.url=
spring.datasource.druid.first.username=root

spring.datasource.druid.first.password=ENC(AfHowGWt0tQ6LXRoJ7GkAlImTKkfDg1Y1Er)

5.通过命令行运行 jasypt-1.9.2.jar 包命令来加密解密

1.在jar包所在目录打开命令行,运行如下加密命令:

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="明文" password=jasypt配置密码 algorithm=PBEWithMD5AndDES

2. 使用刚才加密出来的结果进行解密,执行如下解密命令:

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=jasypt配置密码 algorithm=PBEWithMD5AndD

来源:https://blog.csdn.net/qinzzjs47/article/details/82182623

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com