遇到一个问题, 在Spring Boot 3.x 中使用 Jasypt 时,无法正常解密数据库密码,导致项目无法启动。提示如下
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'spring.datasource.dynamic.datasource.smart-manager.password' to java.lang.String:
Reason: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.dynamic.datasource.smart-manager.password' to java.lang.String
Action:
Update your application's configuration
经排查发现,该问题与 Spring Boot 3.x 的版本兼容性有关。Jasypt 3.x 默认采用强加密算法 PBEWITHHMACSHA512ANDAES_256。而当前使用的是旧算法 PBEWithMD5AndDES,因此需要额外添加配置,为其指定对应的 IV 生成器。
所以除了其他配置配置正确之外,配置文件需要这样写
jasypt:
encryptor:
# 指定加密盐值,生产环境建议通过环境变量或启动参数传入,切勿硬编码
password: ****************
# 关键:指定使用旧算法
algorithm: PBEWithMD5AndDES
# 关键:为旧算法指定对应的IV生成器
iv-generator-classname: org.jasypt.iv.NoIvGenerator