You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Jasypt启动报错:spring.datasource.password绑定失败

Spring Boot + Jasypt 启动绑定失败问题解决方法

问题重现

在Spring Boot应用中使用Jasypt加密数据库密码后,启动时出现属性绑定失败错误,提示无法将spring.datasource.password绑定到java.lang.String,相关配置及错误信息如下:

application.yml 原配置(存在问题):

datasource:
  driver-class-name: org.postgresql.Driver
  url: jdbc:postgresql://localhost:5432/employee_db
  username: postgres
  password: ENC(fpEVpMwMbl4hbcoCKuhrpi...)

# jasypt
jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

docker-compose.yml 原配置(存在问题):

services:
  db:
    image: postgres:14.6-alpine
    environment:
      POSTGRES_DB: employee_db
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ENC(fpEVpMwMbl4hbcoCKuhrpi...)

核心错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource.password' to java.lang.String:

    Reason: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.password' to java.lang.String

问题分析

  1. 配置前缀缺失:application.yml中数据源配置未加spring前缀,Spring无法识别并绑定到spring.datasource.password属性;
  2. 容器密码格式错误:Postgres容器不支持Jasypt加密格式,直接使用ENC(...)会导致数据库密码设置为加密字符串本身;
  3. 加密有效性存疑:加密时使用的密钥、算法可能与启动配置不匹配,导致解密失败无法生成有效字符串。

解决步骤

1. 修正application.yml配置前缀

将数据源配置添加spring前缀,确保Spring能正确识别属性:

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/employee_db
    username: postgres
    password: ENC(fpEVpMwMbl4hbcoCKuhrpi...)

# jasypt
jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

2. 修正docker-compose.yml数据库密码

Postgres容器仅支持明文密码,替换为实际明文密码:

services:
  db:
    image: postgres:14.6-alpine
    environment:
      POSTGRES_DB: employee_db
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 你的明文数据库密码

3. 验证加密密码有效性

使用Jasypt工具验证加密字符串能否正确解密:

# 替换为实际的Jasypt jar路径、加密字符串、密钥和算法
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="fpEVpMwMbl4hbcoCKuhrpi..." password=my-secret algorithm=PBEWithMD5AndDES

若解密失败,需重新加密明文密码:

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="你的明文数据库密码" password=my-secret algorithm=PBEWithMD5AndDES

将生成的新加密字符串替换到application.yml中。

4. 确认Jasypt依赖版本兼容

Spring Boot 2.7.5适配jasypt-spring-boot-starter 3.0.4版本,Maven依赖配置:

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>

5. 重启应用验证

确保IntelliJ IDEA的VM参数-Djasypt.encryptor.password=my-secret配置正确,重启应用即可完成属性绑定并连接数据库。

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 15:30:59