Springboot如何实现对配置文件中的明文密码加密

其他教程   发布日期:2023年08月16日   浏览次数:437

这篇文章主要介绍了Springboot如何实现对配置文件中的明文密码加密的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Springboot如何实现对配置文件中的明文密码加密文章都会有所收获,下面我们一起来看看吧。

示例展示

我们来看一下这个配置:

  1. spring:
  2. # 数据库链接配置
  3. datasource:
  4. url: jdbc:mysql://xx.xx.xx.xx:3306/database
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. username: root
  7. password: "123456"

我们上述的配置

  1. spring.datasource.password
对应的值为
  1. 123456
,这么敏感的信息直接放在配置文件中很不合适,我们要做的就是对应的值改成一个加密的密文,如下:
  1. spring:
  2. # 数据库链接配置
  3. datasource:
  4. url: jdbc:mysql://xx.xx.xx.xx:3306/database
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. username: root
  7. password: "AES(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)"

这样的话,即使该配置文件被有心之人拿去,也不知道真正的数据库密码是啥,也就无法构成对项目的侵害风险;

原理解析

我们为了实现这个功能,需要了解

  1. Spring
的相关扩展点以及对应的数据加解密知识,我们先来看看我们应该通过
  1. Spring
的哪个扩展点进行切入;

我们想要拦截配置数据的话,可以通过实现自定义的

  1. BeanFactoryPostProcessor
来处理:
  1. public class PropertySourcePostProcessor implements BeanFactoryPostProcessor {
  2. private ConfigurableEnvironment environment;
  3. public PropertySourcePostProcessor(ConfigurableEnvironment environment) {
  4. this.environment = environment;
  5. }
  6. @Override
  7. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  8. // 从ConfigurableEnvironment中取出所有的配置数据
  9. MutablePropertySources propertySources = this.environment.getPropertySources();
  10. propertySources.stream()
  11. // 过滤不需要包装的对象
  12. .filter(s -> !noWrapPropertySource(s))
  13. // 包装所有的PropertySource
  14. .map(s -> new EncryPropertySource(s))
  15. .collect(Collectors.toList())
  16. // 替换掉propertySources中的PropertySource
  17. .forEach(wrap -> propertySources.replace(wrap.getName(), wrap));
  18. }
  19. private boolean noWrapPropertySource(PropertySource propertySource) {
  20. return propertySource instanceof EncryPropertySource || StringUtils.equalsAny(propertySource.getClass().getName(), "org.springframework.core.env.PropertySource$StubPropertySource", "org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource");
  21. }
  22. }

基本原理解析如下:

1.通过

  1. ConfigurableEnvironment
取出所有的
  1. PropertySource
并依次遍历;

2.过滤掉不符合我们要求的

  1. PropertySource
,因为
  1. PropertySource
有很多子类,并不是所有的
  1. PropertySource
实例都符合我们包装的要求;

3.对符合要求的

  1. PropertySource
做一层包装,其实就是静态代理;

4.用包装好的

  1. PropertySource
替换掉之前的
  1. PropertySource
实例;

通过上述一系列的操作,我们就可以在

  1. PropertySource
取值的时候做一些自定义的操作了,比如针对密文密码进行解密;

剩下的另一个问题就是加解密的问题,密码学里面有对称加密和非对称加密,这两种加密方式的区别就是对称加密的加密解密都需要同一个密钥,而非对称加密加密的时候需要公钥,解密的时候需要私钥;

了解了对称加密与非对称加密的区别,如果我们使用的是对称加密,那么一定要避免密文和密钥放在同一个地方;

  1. 非对称加密
一定要避免密文和私钥放在同一个地方;

工具介绍

接下来我们要介绍一款专门针对这个需求的

  1. jar
工具,它就是
  1. jasypt
,我们可以去
  1. maven
仓库找到相关的包:
  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot-starter</artifactId>
  4. <version>3.0.5</version>
  5. </dependency>

它的实现原理其实就是我们上面所讲述的,通过自定义

  1. BeanFactoryPostProcessor
  1. ConfigurableEnvironment
中的
  1. PropertySource
实例进行拦截包装,在包装类的实现上做一层解密操作,这样就实现了对密文密码的解密;

导入上述依赖后,该工具就已经自动生效了,我们就可以修改对应的配置了,首先我们先针对该工具做一些配置:

  1. jasypt:
  2. encryptor:
  3. # 密钥
  4. password: ""
  5. property:
  6. # 密文前缀
  7. prefix: ""
  8. # 密文后缀
  9. suffix: ""

在上述配置中,

  1. jasypt.encryptor.password
是一定要配置的,这就是加解密的密钥,默认的加密算法是
  1. PBEWITHHMACSHA512ANDAES_256
;另外
  1. jasypt.encryptor.property.prefix
  1. jasypt.encryptor.property.suffix
分别是密文前缀和密文后缀,是用来标注需要解密的密文的,如果不配置,默认的密文前缀是
  1. ENC(
,密文后缀是
  1. )
;默认情况下,我们的密文如下所示:
  1. spring:
  2. datasource:
  3. password: "ENC(DzANBAhBWXxZqAOsagIBCoaw8FV4gYRbid7G70UEM24=)"

还有一个需要注意的点就是

  1. jasypt.encryptor.password
不能与密文放在一起,我们可以在项目当中通过系统属性、命令行参数或环境变量传递;

实现自定义加解密

如果

  1. jasypt
提供的加解密方式不能满足咱们的项目需求,我们还可以自己实现加解密:
  1. @Bean("jasyptStringEncryptor")
  2. public StringEncryptor jasyptStringEncryptor(){
  3. return new StringEncryptor() {
  4. @Override
  5. public String encrypt(String s) {
  6. // TODO 加密
  7. return null;
  8. }
  9. @Override
  10. public String decrypt(String s) {
  11. // TODO 解密
  12. return null;
  13. }
  14. };
  15. }

注意我们的

  1. BeanName
,默认情况下一定要设置成
  1. jasyptStringEncryptor
,否则不会生效,如果想要改变这个
  1. BeanName
,也可以通过修改这个配置参数来自定义
  1. StringEncryptor
实例所对应的
  1. BeanName
  1. jasypt:
  2. encryptor:
  3. # 自定义StringEncryptor的BeanName
  4. bean: ""

如何生成密文

生成密文的这个操作还是要自个儿通过调用

  1. StringEncryptor
实例来加密生成,可以参考以下代码:
  1. @Component
  2. public class StringEncryptorUtil{
  3. @Autowired
  4. private StringEncryptor encryptor;
  5. public void encrypt(){
  6. String result = encryptor.encrypt("123456");
  7. System.out.println(result);
  8. }
  9. }

毕竟需要加密的操作只需要在项目生命周期中执行一次,所以我们只需要简单地写一个工具类调用一下即可。

以上就是Springboot如何实现对配置文件中的明文密码加密的详细内容,更多关于Springboot如何实现对配置文件中的明文密码加密的资料请关注九品源码其它相关文章!