Azure OAuth2:无法验证访问令牌

问题描述 投票:0回答:1

我正在尝试验证 Azure OAuth2 提供的访问令牌。我正在使用 nimbus 来验证令牌,但是我不断收到“无效签名”错误。

我在某些页面上读到,如果访问令牌包含随机数,那么我将无法验证它,因为它只能由 Microsoft 在内部使用。 我按照此页面上的说明 (https://authguidance.com/azure-ad-troubleshooting/) 获取了没有随机数的 accss 令牌,但它不起作用。

我可以看到 ID 令牌不包含随机数,但访问令牌包含。

有谁知道我如何获得可以使用 nimbus 验证的访问令牌(没有随机数)?

java spring azure oauth-2.0 nimbus
1个回答
0
投票

请注意,如果您验证使用 Microsoft 生成的访问令牌 Graph API 作为范围,你会得到“无效签名”错误 有

nonce
声明

我尝试在我的环境中重现相同的内容并得到以下结果:

我注册了一个 Azure AD 应用程序并添加了 Microsoft Graph

API permissions
如下:

enter image description here

现在我生成了access tokenid token使用授权代码流通过Postman使用以下参数:

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
client_id:<appID>
grant_type:authorization_code
scope: https://graph.microsoft.com/User.Read openid
code:code
redirect_uri: https://jwt.ms
client_secret: <secret>

回复:

enter image description here

当我在 jwt.io 网站上解码上述访问令牌时,我也遇到了 Invalid Signature 错误,因为它有

nonce
声明如下:

enter image description here enter image description here

要在没有 nonce 声明的情况下获取访问令牌,请将

scope
值更改为 custom API 而不是 Microsoft API。

我通过在我的应用程序中选择 Expose an API

 添加了应用程序 ID URI 和名为 
Custom.Read 的新范围,如下所示:

enter image description here

您可以在您的应用程序的我的 APIs 中找到上述范围,具有相同的名称,如下所示:

enter image description here

现在,在您的应用程序的API 权限 中添加该范围,如下所示:

enter image description here

确保授予 admin 同意 添加的权限,如下所示:

enter image description here

为了获得code,我在浏览器中运行授权请求如下:

https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/authorize
?client_id= <appID>
&response_type=code
&redirect_uri= https://jwt.ms
&response_mode=query
&scope=api://<appID>/.default
&state=12345

回复:

enter image description here

现在,我通过 Postman 使用授权代码流将范围更改为 自定义 API 来生成访问令牌,如下所示:

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
client_id:<appID>
grant_type:authorization_code
scope: api://<appID>/Custom.Read openid
code:code
redirect_uri: https://jwt.ms
client_secret: <secret>

回复:

enter image description here

当我在 jwt.io 网站上解码上述访问令牌时,它没有

nonce
声明和签名 验证 成功如下:

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.