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.编写测试类得到加密密码

  1. @Autowired  
  2. StringEncryptor stringEncryptor;  
  3.   
  4. @Test  
  5. public void encryptPwd() {  
  6.     String result = stringEncryptor.encrypt("yourPassword");  
  7.     System.out.println(result);   
  8. }  
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包所在目录打开命令行,运行如下加密命令:

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

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

  1. java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=jasypt配置密码 algorithm=PBEWithMD5AndD 
更多详细的内容可以访问:https://blog.csdn.net/clypm/article/details/79539124

猜你喜欢

转载自blog.csdn.net/yc_low_profile/article/details/80690283
今日推荐