好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring boot配置文件加解密详解

功能介绍

大家都知道在spring boot开发过程中,需要在配置文件里配置许多信息,如数据库的连接信息等,如果不加密,传明文,数据库就直接暴露了,相当于"裸奔"了,因此需要进行加密处理才行。

在项目中使用jasypt-1.9.4.jar包,能够实现对明文进行加密,对密文进行解密。配置相关加密信息,就能够实现在项目运行的时候,自动把配置文件中已经加密的信息解密成明文,供程序使用

下面话不多说了,来一起看看详细的介绍吧

使用说明

1.pom引入依赖

?

1

2

3

4

5

<dependency>

  <groupid>com.github.ulisesbocchio</groupid>

  <artifactid>jasypt-spring-boot-starter</artifactid>

  <version> 2.1 . 1 </version>

</dependency>

2.配置文件application.yaml

?

1

2

3

4

5

6

7

******************加解密相关配置*******************

jasypt:

  encrytor:

  #用来加解密的salt值

  password: 123456

  #用来使用新的算法,默认为org.jasypt.salt.noopivgenerator,这样的话我们就无法使用命令行中生成的密文

  ivgeneratorclassname: org.jasypt.salt.randomivgenerator

参数解释:

password:加密时候要使用salt值 对于ivgeneratorclassname,jara包中封装类默认为org.jasypt.salt.noopivgenerator,这个时候我们如果使用junit生成密文,那么只会生成24位密钥,与命令行中用命令生成的不一样,后面会详细讲解。

3.代码解析

首先我们需要知道的事加解密的方法,只有知道了如何加密才能够在配置文件中设置相关参数的密文,这里涉及到两种方式的加密:

a.命令行加密

如果我们项目上线了,需要修改配置文件中的信息,这个时候我们可能要通过命令行的方式去加密(前提:保证你的salt值和你的项目中定义的一致)

i.找到maven仓库本地地址,如:c:\users\kfzx-xuming\.m2\repository 在这里面找到jasypt-1.9.4.jar所在位置

ii.进入文件夹,运行cmd命令

加密:java -cp jasypt-1.9.4.jar org.jasypt.intf.cli.jasyptpbestringencryptioncliinput=pass1234password=12345algorithm=pbewithmd5anddes

参数说明:

input:加上需要加密的明文 password:加上salt值(需要和项目中的application.yaml的password 一致) algorithm:加上加密算法(默认使用的就是pbewithmd5anddes)

这个时候我们可以看到下面的加密结果:

下面的output中就是我们对明文pass1234使用salt值为12345加密的结果

解密:java -cp jasypt-1.9.4.jar org.jasypt.intf.cli.jasyptpbestringdecryptioncli input=pdfvckrynvoktpej+081g70kzvwv2alrtok2ejrjkksnmbu4c4ix+q== password=12345 algorithm=pbewithmd5anddes

这个时候我们可以看到解密结果:

b.在eclipse中用junit运行代码对明文加密解密

前提已经在配置文件中配置了jasypt相关信息

jasypt提供了封装类stringencryptor,可以通过代码来加解密,我们可以使用这个类运行相关方法

junit相关代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import org.jasypt.encryption.stringencryptor;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.boot.context.embedded.embeddedservletcontainercustomizer;

import org.springframework.boot.test.context.springboottest;

import org.springframework.test.context.junit4.springjunit4classrunner;

 

import static sun.plugin.javascript.navig.jstype.embed;

 

@runwith (springjunit4classrunner. class )

@springboottest

public class jasypttest {

 

  @autowired

  stringencryptor encryptor;

  //加密

  @test

  public void getpass(){

  string name = encryptor.encrypt( "hello" );

  system.out.println( "加密结果:" +name); //解密

  @test

  public void passdecrypt(){

  string username = encryptor.decrypt( "7ubc9fvlpl05ipepzgsdt6qcjuq9hvdyc0vuigp4hy=" );

  system.out.println( "解密结果:" +username);

  }

}

运行结果如下:

这个时候我们就得到了想要的密文,直接粘贴到配置文件中即可

使用方法如下:

i.在配置文件application.yaml相关位置把明文替换成密文,用enc()包裹:

?

1

2

3

4

************** 加解密相关测试配置信息***************

test:

  code:

  username: enc(pdfvckrynvoktpej+081g70kzvwv2alrtok2ejrjkksnmbu4c4ix+q==)

ii.在相应的位置直接读取使用即可,下面我们写一个controller类测试一下运行解密的效果:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import org.springframework.beans.factory.annotation.value;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.restcontroller;

 

@controller

public class jasyptcontroller {

 

  @value ( "${cmd.username}" )

  private string cmdusername;

 

  @requestmapping ( "/hello" )

  public string testjasypt() {

  return cmdusername;

  }

}

这个时候我们启动项目就能够看到我们再配置中设置的密文对应的明文

至此配置文件的加解密的使用方法就介绍完了

4.补充说明

对于上述配置文件中的ivgeneratorclassname再进行一个详细的介绍

对于上述的junit中使用的stringencryptor封装类,他是可以通过读取配置文件中的信息进行加解密相关参数进行初始化,通过阅读远吗,我们可以发现,初始化config的时候会跳转到如下的地方进行设置:

如果在配置参数中没有设置ivgeneratorclassname,那么默认就是org.jasypt.salt.noopivgenerator,那么在运行加解密的时候就会生成一个24位的密文,如图:

但是我们可以看到上面用命令行生成的却比这个厂,这个时候如果我们把命令行中生成的密文粘贴到配置文件中则springboot就会启动不了,junit也会报错解析,把这个密文用命令解析发现也会报错

这个说明命令行中的加解密不是通过stringencryptor类来操作的,那是走那边的呢?

通过查阅资料我们发现了下面的代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package cn.linjk.ehome;

 

import org.jasypt.encryption.pbe.standardpbestringencryptor;

import org.jasypt.encryption.pbe.config.environmentpbeconfig;

import org.junit.test;

 

public class jasypttest {

  @test

  public void testencrypt() throws exception {

  standardpbestringencryptor standardpbestringencryptor = new standardpbestringencryptor();

  environmentpbeconfig config = new environmentpbeconfig();

 

  config.setalgorithm( "pbewithmd5anddes" );  // 加密的算法,这个算法是默认的

  config.setpassword( "12345" );   // 加密的密钥

  standardpbestringencryptor.setconfig(config);

  string plaintext = "hello" ;

  string encryptedtext = standardpbestringencryptor.encrypt(plaintext);

  system.out.println(encryptedtext);

  }

}

这个时候我们运行一下,得到下面的结果:

把这个密文用命令进行解密发现也是成功的,查看源码(下图):可以看出,命令行如果没有设置ivgeneratorclassname那么默认就会new randomivgenerator,就是这一步导致了生成了不一样的密文:

综上所述,结合场景,如果我们是项目需要上线了,不方便运行junit去生成密文,填入配置文件,需要用命令行对明文加密,那一定要在配置文件中设置ivgeneratorclassname值!!!

公司电脑没有办法用外网上,图片用手机拍的,如果感觉模糊,请见谅,大家相互学习~~~~~~

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://HdhCmsTestcnblogs测试数据/charles8866/p/10478796.html

查看更多关于Spring boot配置文件加解密详解的详细内容...

  阅读:55次