1

当我通过授权码 ( authContext.acquireTokenByAuthorizationCode) 获得令牌时,我得到一个已签名并具有正确标头的 JWT (idToken):

{
  "typ": "JWT",
  "alg": "RS256",
  "x5t": "wLLmYfsqdQuWtV_-hnVtDJJZM3Q",
  "kid": "wLLmYfsqdQuWtV_-hnVtDJJZM3Q"
}

但是当我使用刷新令牌获取新令牌(authContext.acquireTokenByRefreshToken(...))时,它返回一个未签名的 JWT:

{
  "typ": "JWT",
  "alg": "none"
}

我如何让它给我一个签名的 JWT?

return authContext.acquireTokenByRefreshToken( refreshToken, new ClientCredentials( clientId, clientSecret ), null );

4

1 回答 1

1

我没有在我这边重现您的问题。我按照本教程成功获取Authentication code和获取access tokenrefresh token使用以下代码。请参考它。

import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;

import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class GetTokenByAuthenticationCode {

    private static final String APP_ID = "***";
    private static final String APP_SECRET = "***";
    private static final String REDIRECT_URI = "http://localhost:8080";
    private static final String tenant = "***";

    public static void main(String[] args) throws Exception {

        String authority = "https://login.microsoftonline.com/" + tenant + "/oauth2/authorize";
        ExecutorService service = Executors.newFixedThreadPool(1);

        String code = "***";

        AuthenticationContext context = new AuthenticationContext(authority, true, service);

        URI url = new URI(REDIRECT_URI);

        Future<AuthenticationResult> result = context.acquireTokenByAuthorizationCode(
                code,
                url,
                new ClientCredential(APP_ID, APP_SECRET),
                null
        );
        String token = result.get().getAccessToken();
        System.out.println(token);
        String refreshToken = result.get().getRefreshToken();
        System.out.println(refreshToken);


        Future<AuthenticationResult> result1 = context.acquireTokenByRefreshToken(
                refreshToken,
                new ClientCredential(APP_ID, APP_SECRET),
                null
        );

        String tokenNew = result1.get().getAccessToken();
        String refreshTokenNew = result1.get().getRefreshToken();
        System.out.println(tokenNew);
        System.out.println(refreshTokenNew);
    }
}

解码:

在此处输入图像描述


更新答案:

首先,很抱歉这个错误。我换成getIdTokengetAccessToken,结果和你一样。然后我在Authorize access to Azure Active Directory web applications using OAuth 2.0 code grant flow中搜索了响应参数,你可以找到id_token参数的语句。

表示 ID 令牌的未签名 JSON Web 令牌 (JWT)。应用程序可以 base64Url 解码此令牌的片段以请求有关登录用户的信息。应用程序可以缓存值并显示它们,但不应依赖它们来获得任何授权或安全边界。

因此,id 令牌只是一个不能依赖的段。如需获取完整的 id 令牌,请参考openId 流程

于 2018-12-12T05:33:55.090 回答