简体   繁体   English

无法使用 nimbus 验证来自 azure 的访问令牌签名

[英]Can't verify access token signature from azure using nimbus

following this example I wrote some code to validate an access token return by implicit flow from azure.在这个示例之后,我编写了一些代码来验证通过隐式流从 azure 返回的访问令牌。

RemoteJWKSet remoteJWKSet = new RemoteJWKSet(new URL(jwksUri));
JWSKeySelector keySelector = new JWSVerificationKeySelector(JWSAlgorithm.RS256, remoteJWKSet);

ConfigurableJWTProcessor jwtProcessor = new DefaultJWTProcessor<>();

jwtProcessor.setJWTClaimsSetVerifier(new DefaultJWTClaimsVerifier(
      new JWTClaimsSet.Builder().issuer("https://sts.windows.net/3283e312-f73b-47d0-81c6-75e3ac726c21/").build(),
                new HashSet<>(Arrays.asList("sub", "iat", "exp", "scp"))));

jwtProcessor.setJWSKeySelector(keySelector);

JWTClaimsSet claimsSet = jwtProcessor.process(accessToken.getValue(), null);

But the verification fails and I get:但是验证失败,我得到:

com.nimbusds.jose.proc.BadJWSException: Signed JWT rejected: Invalid signature
    at com.nimbusds.jwt.proc.DefaultJWTProcessor.process(DefaultJWTProcessor.java:378)
    at com.nimbusds.jwt.proc.DefaultJWTProcessor.process(DefaultJWTProcessor.java:303)
    at com.nimbusds.jwt.proc.DefaultJWTProcessor.process(DefaultJWTProcessor.java:294)

I think I don't need DefaultJWTClaimsVerifier part, but removing it doesn't change anything.我想我不需要DefaultJWTClaimsVerifier部分,但删除它不会改变任何东西。 I iust keept it to stick with the example.我只是坚持这个例子。

Do you know why this happends?你知道为什么会这样吗?

Thanks for your help.谢谢你的帮助。

PS: Can't verify with jwt.io. PS:无法通过 jwt.io 进行验证。 I pasted access_token and the first entry from "keys" section from jwk.我粘贴了 access_token 和 jwk 的“keys”部分的第一个条目。

You need to expose an API scope in Azure, and get the client to use that.您需要在 Azure 中expose an API scope scope,并让客户端使用它。 Also ensure that there is no nonce field in the JWT header of the access token.还要确保访问令牌的 JWT header 中没有nonce字段。 My blog post has further info.我的 博客文章有更多信息。

AZURE AD BEHAVIOR AZURE 广告行为

The above behavior is quite specific to Microsoft, and is required when using Azure AD as a provider:上述行为非常特定于 Microsoft,并且在使用 Azure AD 作为提供者时是必需的:

  • Tokens with a nonce field in the JWT header are designed only for MS APIs, eg Graph, and use an in-house validation mechanism.在 JWT header 中带有 nonce 字段的令牌专为 MS API(例如 Graph)设计,并使用内部验证机制。 The intent is for these to always fail validation in custom APIs.目的是让这些在自定义 API 中始终无法通过验证。

  • Tokens for your own custom APIs must be retrieved via clients that request custom scopes.您自己的自定义 API 的令牌必须通过请求自定义范围的客户端检索。 Note that the OAuth client configured in Azure AD can be a logical entry, rather than needing to maintain one for each individual API.注意Azure AD中配置的OAuth客户端可以是一个逻辑入口,而不需要为每个API单独维护一个。

I believe the MS behavior is based on OAuth resource indicators , though my personal preference is to use more mainstream techniques of scopes , claims and audience checks when receiving access tokens in APIs.我相信 MS 行为是基于OAuth 资源指示器的,尽管我个人的偏好是在 API 中接收访问令牌时使用更主流的范围声明和受众检查技术。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Active Directory 应用程序服务无法使用用户凭据/令牌连接到 Azure Keyvault - Azure Active Directory app service can't connect to Azure Keyvault using user creds/token 验证来自 Azure AD 的机密令牌时无法指定 JWKSet URL - Can't specify JWKSet URL when validating confidential token coming from Azure AD 您能否通过 web 请求使用用户名和密码而不使用 client_id 获得 Azure 的访问令牌? - Can you get an access token for Azure with a username and password and without using a client_id via web request? 在 Node.js 中验证 Azure OAuth 令牌 - Verify Azure OAuth token in Node.js 如何使用 `NextAuth.js` 从 Azure B2C 检索 JWT 访问令牌 - How to retrieve a JWT access token from Azure B2C using `NextAuth.js` Scope 未添加到从 Azure 广告返回的访问令牌 - Scope is not being added to Access Token returned from Azure Ad 如何从客户端获取登录用户的 access_token? 使用Azure AD B2C混合流 - How to get access_token from client side for signed in user? using Azure AD B2C hybrid flow Azure AKS/容器应用程序无法使用托管标识访问密钥保管库 - Azure AKS/Container App can't access Key vault using managed identity 无法访问部署在 AKS 集群上的我的应用程序,来自 azure 公共 ip - Can't access my app deployed on an AKS cluster, from the azure public ip Azure Active Directory 身份验证 401,Bearer Token 签名无效 - Azure Active Directory Authentication 401, Bearer Token The signature is invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM