gpt4 book ai didi

azure - 验证 Azure AD JWT 访问 token 时出错

转载 作者:行者123 更新时间:2023-12-02 06:18:39 25 4
gpt4 key购买 nike

我有一个使用 Msal 库获取的 Azure AD JWT token ,但当我尝试验证此 token 时出现错误:

客户端:Sharepoint Web 部件

const config = {
auth: {
clientId: "xxxxx",
authority: "https://login.microsoftonline.com/yyyyyy"
}
};

const myMSALObj = new UserAgentApplication(config);

let accessTokenRequest = {
scopes: ["user.read"],
loginHint: this.context.pageContext.user.loginName,
extraQueryParameters: {domain_hint: 'organizations'}
}

myMSALObj.acquireTokenSilent(accessTokenRequest).then(
function(accessTokenResponse) {
// Acquire token silent success
let accessToken = accessTokenResponse.accessToken;
<小时/>

另一方面,我有一个服务器应用程序(Java),其中验证访问 token

验证器:

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.6.2</version>
</dependency>

<dependency>
<groupId>com.auth0</groupId>
<artifactId>jwks-rsa</artifactId>
<version>0.11.0</version>
</dependency>

代码

 String token="<your AD token>";
DecodedJWT jwt = JWT.decode(token);
System.out.println(jwt.getKeyId());

JwkProvider provider = null;
Jwk jwk =null;
Algorithm algorithm=null;

try {
provider = new UrlJwkProvider(new URL("https://login.microsoftonline.com/common/discovery/keys"));
jwk = provider.get(jwt.getKeyId());
algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
algorithm.verify(jwt);// if the token signature is invalid, the
method will throw SignatureVerificationException
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JwkException e) {
e.printStackTrace();
}catch(SignatureVerificationException e){
System.out.println(e.getMessage());
}
<小时/>

我的问题是,当我尝试验证此 token 时,出现此错误:使用算法验证时, token 的签名无效:SHA256withRSA

我被这个问题困扰了,如果 token 是正确的,为什么我会遇到这个错误?

问候

最佳答案

我注意到范围是 user.read,这意味着该 token 适用于 Microsoft Graph API。

请注意:

If you're a client getting a token for Graph, assume that it's an encrypted string that you should never look at - sometimes it will be. We use a special token format for Graph that they know how to validate - you shouldn't be looking at access tokens if they're not for you.

您可以使用此访问 token 直接调用 Microsoft Graph API,如果 token 错误,您将从 Microsoft API 服务器获取响应。

引用:

https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/609#issuecomment-529537264

关于azure - 验证 Azure AD JWT 访问 token 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60953983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com