java - 如何使用 ADAL JS 颁发的隐式授权 token 从 Web API 访问图形 API

标签 java api authentication azure-active-directory

我有一个用 Java 构建的 Web API,可以将数据库信息返回给 SPA。在传递响应之前,我需要使用 AAD Graph API 检查用户的组信息。现在 Web API 接受请求并读取用户的 token (eyJ...)。

应用程序准备好代表用户向 Graph API 发送请求的后续步骤是什么?

我已尝试使用用户 token 发送带有 Authorization: Bearer ey... header 的请求,但收到了 Authentication_MissingOrMalformed 错误。我还尝试了对应用程序 list 和委派权限的各种编辑,但没有成功。

最佳答案

您的 API 收到的访问 token 用于您的 API。您需要代表当前用户为 Azure AD Graph API 获取访问 token 。

幸运的是,这正是代表流程的目的。来自 Authentication Scenarios for Azure AD :

Delegated User Identity with OAuth 2.0 On-Behalf-Of Draft Specification

The flow discussed below assumes that a user has been authenticated on another application (such as a native application), and their user identity has been used to acquire an access token to the first-tier web API.

  1. The native application sends the access token to the first-tier web API.
  2. The first-tier web API sends a request to Azure AD’s token endpoint, providing its client ID and credentials, as well as the user’s access token. In addition, the request is sent with an on_behalf_of parameter that indicates the web API is requesting new tokens to call a downstream web API on behalf of the original user.
  3. Azure AD verifies that the first-tier web API has permissions to access the second-tier web API and validates the request, returning a JWT access token and a JWT refresh token to the first-tier web API.
  4. Over HTTPS, the first-tier web API then calls the second-tier web API by appending the token string in the Authorization header in the request. The first-tier web API can continue to call the second-tier web API as long as the access token and refresh tokens are valid.

请务必配置您的 API 以请求 Azure AD Graph API 的正确权限集。

编辑:如果您自己构建 token 请求,则您的 API 向 Azure AD 发出的代表当前用户获取图形 API 新 token 的请求将是一个 POST反对:

https://login.microsoftonline.com/{tenant-id}/oauth2/token

正文中有以下参数(未编码,为了便于阅读,实际上这些参数当然是 application/x-www-form-urlencoded):

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&
requested_token_use=on_behalf_of&
assertion={access-token}&
client_id={api-client-id}&
client_secret={api-client-secret}&
resource=https://graph.windows.net&
scope=openid

其中 {tenant-id} 是目录标识符(域名或 Guid 值),{access-token} 是您的 SPA 提供给您的访问 token API(您要交换的 API 访问 token ),{api-client-id} 是您的 API 的客户端 ID,而 {api-client-secret 是 API 的 secret 密码凭证。

(请注意,为简单起见,此示例使用密码凭据 (client_secret) 对 API 进行身份验证,尽管它很可能使用由客户端证书签名的断言。)

关于java - 如何使用 ADAL JS 颁发的隐式授权 token 从 Web API 访问图形 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37686673/

相关文章:

Android - Magento REST Api 无法正确响应

c# - 在 ASP.NET 项目中添加特定的身份验证

authentication - Nextjs next-auth sessionProvider 不读取 session 属性 Typescript

java长整顿到小数点后两位绝对值

java - 线程 "main"org.openqa.selenium.NoSuchElementException : When trying to select an element from a pop-up using selenium 中的异常

java - 如何在 Android 应用程序中请求特定的文本大小?

php - 如何将 Controller 索引函数的参数传递给模型的函数?拉维

java - 是否可以只针对一个 Java 类运行 FindBugs?

c# - .NET API 文档是否应该定义/公开枚举成员的文字(数字)值?

javascript - AngularJS 身份验证和基于 XSRF token