当前位置:   article > 正文

SpringCloud框架源码EnvironmentDecryptApplicationInitializer冲突解决

environmentdecryptapplicationinitializer

问题描述

配置文件需要配置数据库加密密码,采用RSA加密算法,但是统一需要 加{cipher}{rsa}前缀,不加前缀前启动正常。加了前缀后并且已经在代码里边处理替换掉前缀但是仍然报错:

  1. java.lang.IllegalStateException: Cannot decrypt: key=spring.datasource.sydev.password
  2. at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:298)
  3. at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.lambda$decrypt$0(EnvironmentDecryptApplicationInitializer.java:276)
  4. at java.util.LinkedHashMap.replaceAll(LinkedHashMap.java:694)
  5. at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:271)
  6. at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:196)
  7. at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.initialize(EnvironmentDecryptApplicationInitializer.java:130)
  8. at org.springframework.cloud.bootstrap.BootstrapApplicationListener$DelegatingEnvironmentDecryptApplicationInitializer.initialize(BootstrapApplicationListener.java:441)
  9. at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:626)
  10. at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:370)
  11. at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
  12. at com.ztesoft.SmartServiceApplication.main(SmartServiceApplication.java:38)
  13. Caused by: java.lang.UnsupportedOperationException: No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly?
  14. at org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration$FailsafeTextEncryptor.decrypt(EncryptionBootstrapConfiguration.java:165)
  15. at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:283)
  16. ... 10 common frames omitted

解决办法

查看bug提示的EnvironmentDecryptApplicationInitializer源码,发现他是springcloud内置的加密解密方法,打断点定位到如下方法:

于是查看ENCRYPTED_PROPERTY_PREFIX:

what?

刚好跟配置文件的配置密码前缀冲突,所以思路是重写下改类,重写的方式时包路径形同,记住需要启动的服务都需要重写一遍,因为这个是springcloud里边的,类加载在自己服务之前,所以你自己只在代码截取还不能起作用,都还没类加载到你代码就崩了

第一步:

在需要启动服务的的java文件夹下服务重写下类,新建下包和class:

第二步:

修改源码的ENCRYPTED_PROPERTY_PREFIX为其他任意值,尽量复杂一点避免以后冲突:

第二步:重写下源码的decrypt方法,替换下前缀

  1. private void decrypt(Map<String, Object> properties) {
  2. properties.replaceAll((key, value) -> {
  3. String valueString = value.toString();
  4. if (!valueString.startsWith(ENCRYPTED_PROPERTY_PREFIX)) {
  5. return valueString.replace(RSA_PRE_FIX, "");
  6. }
  7. return decrypt(key, valueString);
  8. });
  9. }

第三步:在自己代码逻辑里边,调用加密方法的地方替换下前缀,......

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/155042
推荐阅读
相关标签
  

闽ICP备14008679号