【问题标题】:Validation of Azure AD token signature is invalid. The Token's Signature resulted invalid when verified using the Algorithm: SHA256withRSAAzure AD 令牌签名的验证无效。使用算法验证时,令牌的签名无效:SHA256withRSA
【发布时间】:2021-08-10 20:39:09
【问题描述】:

我在验证 azure Active Directory 令牌时遇到问题。我使用我的 application_id 以及用户的用户名和密码接收令牌。然后我会验证它,但它会导致签名无效。验证的代码片段如下:

    // Request access token from AAD
    IAuthenticationResult result = getAccessToken(userName, password);
    String auth = result.accessToken();
    DecodedJWT jwt = JWT.decode(auth);
    JwkProvider provider = null;
    Jwk jwk = null;
    Algorithm algo = null;
    try {
        provider = new UrlJwkProvider(new URL("https://login.microsoftonline.com/common/discovery/keys"));
        jwk = provider.get(jwt.getKeyId());
        System.out.println(jwk.getPublicKey());
        algo = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
        algo.verify(jwt);
    }  catch (SignatureVerificationException e) {

        System.out.println(e.getMessage());

    } catch (JwkException e) {
        e.printStackTrace();
    }

我用这个方法检索令牌信息

private static IAuthenticationResult getAccessToken(String userName, String password)
        throws MalformedURLException, InterruptedException, ExecutionException {

    PublicClientApplication pca = PublicClientApplication.builder(
            APP_ID).
            authority(AUTHORITY).build();

    String scopes = "User.Read";
    UserNamePasswordParameters parameters = UserNamePasswordParameters.builder(
            Collections.singleton(scopes),
            userName,
            password.toCharArray()).build();
    IAuthenticationResult result = pca.acquireToken(parameters).get();
    return result;
}

程序总是会捕获 SignatureVerificationException。我尝试使用 jwt.io 手动验证令牌,在那里我粘贴了当我将 kid 声明与 https://login.microsoftonline.com/common/discovery/keys 上的声明进行比较时获得的证书,但结果我也得到了无效签名。我的令牌有问题吗,因为验证过程都说签名在 jwt.io 和我的 java 程序中无效,还是有其他方法可以验证 Azure AD 令牌?

编辑:解决方案是将范围从“User.Read”更改为“[client_id]/.default”。

【问题讨论】:

标签: java azure validation jwt token


【解决方案1】:

因为您获取的是自定义 api 的令牌,而不是 ms graph api 的令牌。所以你需要将scope设置为:{api app client_id}/.default

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 2018-01-01
    • 1970-01-01
    • 2017-05-19
    • 2011-04-08
    • 1970-01-01
    • 2019-01-11
    • 2015-03-19
    相关资源
    最近更新 更多