先行一步
VMware 提供培训和认证,助您加速进步。
了解更多无论您是构建 Web API、移动前端还是传统的桌面应用程序,身份和访问管理始终是软件开发中基础且核心的部分。Azure 提供了一个很棒的平台,可以赋能您的应用程序开发之旅,因为它不仅提供基于云的身份服务,还与 Azure 生态系统的其他部分深度集成。Spring Security 凭借强大的抽象和可扩展的接口,让保护您的 Spring 应用变得容易。然而,Spring Framework 虽强大,但并非针对特定的身份提供者量身定制。Azure Active Directory 的 Azure Spring Boot Starter 是 Microsoft 和 VMware 合作的成果,旨在提供将应用程序连接到 Azure AD 租户并使用 Azure Active Directory 保护资源 API 的最佳方式。举例来说,在某些场景下,您可能需要针对多个资源服务器进行授权,您只需在应用程序配置中包含多个授权客户端即可轻松实现。
要开始使用 Azure Active Directory 的 Azure Spring Boot Starter,请将您的应用程序连接到 Azure AD 租户,并在您的 application.yml
中包含以下配置
azure:
activedirectory:
tenant-id: xxxxxx-your-tenant-id-xxxxxx
client-id: xxxxxx-your-client-id-xxxxxx
client-secret: xxxxxx-your-client-secret-xxxxxx
authorization-clients:
graph:
scopes:
- https://graph.microsoft.com/User.Read
- https://graph.microsoft.com/Directory.AccessAsUser.All
在您的 Controller 中获取 OAuth2AuthorizedClient
@GetMapping("/graph")
@ResponseBody
public String graph(
@RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient client
) {
// Now you can use the access token to access a graph URI
}
更多详情,请参阅我们的示例项目。
我们来看一下该 starter 最近的一些增强功能
身份平台提供了 Microsoft API,可与许多知名的 Microsoft SaaS 应用程序交互。例如,可以使用 Graph API 与 Office 365 交互,使用 Azure DevOps 与 Azure DevOps Server 通信,使用 Azure Batch 在云中调度 HPC 应用程序。要使用 Graph API,首先请配置您的 application.yml
azure:
activedirectory:
authorization-clients:
graph:
scopes:
- https://graph.microsoft.com/User.Read
- https://graph.microsoft.com/Directory.AccessAsUser.All
然后在您的 Controller 中获取 OAuth2AuthorizedClient
。
您可以根据需要逐步批准您的应用程序访问资源和 API。当您希望对访问管理有更精细的控制,而不是一次性授予所有作用域时,这种行为是可取的。要在 Web 应用程序中使用增量同意,请配置您的 application.yml
azure:
activedirectory:
authorization-clients:
arm:
on-demand: true
scopes: https://management.core.windows.net/user_impersonation
当应用程序尝试获取相应的 OAuth2AuthorizedClient
时,会触发增量同意。它也可以通过访问 URL:/login/oauth2/code/{authorization-client}
来触发
在您的配置属性中添加 azure.activedirectory.post-logout-redirect-uri
,当用户执行注销时,您的应用程序将自动注销所有活动会话,然后将用户重定向到 logout-redirect-uri
。
更多详情请参阅这个 Web 应用程序示例项目。(此示例包含所有 3 个场景)
在 OAuth 2.0 中,资源服务器是使用令牌保护底层资源的应用程序。在此版本中,我们添加了对 Audience 和 Issuer 的验证,以确保预期的 Audience 和 Issuer 得到遵守。有关详细信息,请参阅资源服务器示例项目。
当您的应用程序使用令牌调用 API A,而 API A 又调用 API B 时,在访问 API B 之前需要不同的令牌。OAuth 2.0 提供了一种 On-Behalf-Of (OBO) 流,专门用于处理这种常见场景。借助 Azure Active Directory 的 Azure Spring Boot Starter,您可以委托用户身份请求并在请求链中进行传播。
有关详细信息,请参阅带 OBO 的资源服务器示例项目。
为与 Azure SDKs 的其余部分保持一致,从 3.x 版本开始,Azure Spring Boot starters 的包 ID 已从 azure-[startername]-spring-boot
更名为 azure-spring-boot-[startername]
。
3.0.0 版本已发布,支持 Spring Boot 2.2.x 和 2.3.x