springboot给mysql表加锁_jasypt使用SpringBoot如何实现对数据库的密码进行加密

jasypt使用SpringBoot如何实现对数据库的密码进行加密

发布时间:2020-11-19 15:36:16

来源:亿速云

阅读:80

作者:Leah

这期内容当中小编将会给大家带来有关jasypt使用SpringBoot如何实现对数据库的密码进行加密,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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

com.github.ulisesbocchio

jasypt-spring-boot-starter

1.14

com.github.ulisesbocchio

jasypt-spring-boot-starter

1.5-java7

com.github.ulisesbocchio

jasypt-spring-boot-starter

1.5-java6

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

运行命令后如图:

6260f1c1badb74cdb45955db60d9de69.png

----OUTPUT----------------------Ekgkm+TzSmf7w7bcr90gMV6MAwj0waW+

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

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

d65b6346803ed746ed1820d7cd979342.png

# 数据库密码加密配置

jasypt:

encryptor:

password: allanpassword

spring MySQL密码写法:

password: ENC(Ekgkm+TzSmf7w7bcr90gMV6MAwj0waW+)

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

e4fc41b87481317f069e9b1f305dd1d7.png

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

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

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

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

com.github.ulisesbocchio

jasypt-spring-boot-starter

1.8

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

上述就是小编为大家分享的jasypt使用SpringBoot如何实现对数据库的密码进行加密了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。


版权声明:本文为weixin_33911375原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。