修改Spring Boot版Azure Function的application.yaml,用用户分配托管身份连接Azure PostgreSQL弹性服务器
使用用户分配托管身份连接Azure PostgreSQL的application.yaml修改方案
以下是针对你的Spring Boot Azure Function应用,使用用户分配托管身份连接Azure PostgreSQL弹性服务器的具体yaml修改步骤:
1. 移除不必要的端口配置
Azure Function的运行端口由平台自动管理,无需手动指定,可删除或注释掉原端口配置:
# server: # port : 8081
2. 修改JDBC URL添加Azure AD身份验证参数
在原有JDBC URL后追加&authentication=azuread参数,告知PostgreSQL驱动使用Azure AD身份验证:
spring: datasource: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://${DB_SERVER_NAME}.postgres.database.azure.com:5432/${DB_NAME}?sslmode=require&authentication=azuread
3. 删除传统用户名密码配置
用户分配托管身份通过Azure AD令牌完成身份验证,无需配置用户名和密码,直接删除以下两行:
# username: ${DB_USER} # password: ${DB_PASSWORD}
4. 添加用户分配托管身份配置
引入Spring Cloud Azure托管身份配置,指定你创建的用户分配托管身份的Client ID(通过环境变量注入):
spring: cloud: azure: managed-identity: client-id: ${USER_ASSIGNED_MI_CLIENT_ID}
最终完整的application.yaml示例
spring: datasource: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://${DB_SERVER_NAME}.postgres.database.azure.com:5432/${DB_NAME}?sslmode=require&authentication=azuread jpa: hibernate: ddl-auto: update properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect jdbc: lob: non_contextual_creation: true cloud: azure: managed-identity: client-id: ${USER_ASSIGNED_MI_CLIENT_ID}
额外注意事项
- 需在Azure PostgreSQL弹性服务器中创建与用户分配托管身份对应的数据库用户,并授予所需的数据库访问权限
- 项目中需引入
azure-spring-boot-starter-jdbc-postgresql依赖,以支持托管身份的自动令牌获取和身份验证流程
内容的提问来源于stack exchange,提问作者Nandishwar Garg
相关产品推荐
相关产品推荐

