首页 > 解决方案 > Jasypt 无法使用 Spring 从系统环境变量中解密密码

问题描述

我有 jasypt 密码和 application.yml 中定义的加密密码,如下所示:

jasypt:
  encryptor:
    password: ${secretKey}

spring:
   datasource: 
       password: ENC(${password})

并为此使用@Value:

@Value("${spring.datasource.password}")
private String springPassword;

我已经在我的环境变量中定义了 secretKey 和密码。但是当我启动这个 spring boot 应用程序时,它会抛出错误:

Caused by: org.springframework.cache.Cache$ValueRetrievalException: Value for key 'spring.datasource.password' could not be loaded using 'com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource$$Lambda$209/172678484@5ae15'

如果我对两个键都进行硬编码,那么它就可以正常工作。

任何帮助,将不胜感激。

标签: javaspringspring-bootjasypt

解决方案


我知道为时已晚,但仍然如此。对我来说,将 ENC() 方法作为环境变量的一部分传递是有效的。

jasypt:
  encryptor:
    password: ${secretKey}

spring:
   datasource: 
       password: ${PASSWORD}

在设置环境变量时

export PASSWORD=ENC(yourPassword)

推荐阅读