首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >刷新后收到的Azure AD Adal4j令牌未签名

刷新后收到的Azure AD Adal4j令牌未签名
EN

Stack Overflow用户
提问于 2017-09-29 14:44:16
回答 2查看 521关注 0票数 1

我正在使用Azure AD OpenID连接框架为我的基于web的java应用程序开发身份验证服务。我指的是adal4j-1.2.0.jar,身份验证是根据行为进行的。我正在获取JWT声明,并能够验证它。

但是,当发生60分钟的会话超时时,当我尝试使用刷新令牌获取新的令牌声明时,新的令牌不是经过签名的JWT。他们是Plain JWT。

我正在使用下面的调用来使用我缓存的初始刷新令牌来获取令牌。

代码语言:javascript
复制
acquireTokenByRrefreshToken(refreshtoken, credential,null,null)

为了验证令牌,我使用如下代码

代码语言:javascript
复制
IDtokenValidator validator =  new IDTokenValidator(issuer,clientID, JWSAlgo,URL)
validator.validate(idToken, exoectedNoounce); //this line throws badjwtexception signed ID token expected

是否有人可以帮助我了解如何兑换刷新令牌以获得新的签名令牌。或者在兑换令牌之后,新的令牌始终是Plain JWT。

EN

回答 2

Stack Overflow用户

发布于 2017-09-29 15:28:12

我相信,您正在使用隐式授权流程来获取token.You正在从授权端获取令牌的流程,您将不会获得刷新token.Either您需要在会话到期后获得新的令牌或创建一个隐藏的框架,可以在会话到期之前获取令牌。

票数 1
EN

Stack Overflow用户

发布于 2017-10-04 16:38:04

您可以参考official doc来获取access tokenrefresh token by code grant flow

实际上,adal4j中的方法是通过HTTP REST API实现的,因此您可以参考下面的代码来请求AuthorizationCode

代码语言:javascript
复制
public static void getAuthorizationCode() throws IOException {

        String encoding = "UTF-8";
        String params = "client_id=" + clientId 
                + "&response_type=" + reponseType
                + "&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F"
                + "&response_mode=query"
                + "&resource=https%3A%2F%2Fgraph.windows.net"
                + "&state=12345";
        String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
        byte[] data = params.getBytes(encoding);
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));
        conn.setConnectTimeout(5 * 1000);
        OutputStream outStream = conn.getOutputStream();
        outStream.write(data);
        outStream.flush();
        outStream.close();
        System.out.println(conn.getResponseCode());
        System.out.println(conn.getResponseMessage());

        BufferedReader br = null;
        if (conn.getResponseCode() != 200) {
            br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
        } else {
            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        }
        System.out.println("Response body : " + br.readLine());
    }

然后,您可以使用获得的AuthorizationCode获取access token,并使用以下代码获取刷新代码。

代码语言:javascript
复制
public static void getToken(String refreshToken) throws IOException {

        String encoding = "UTF-8";
        String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
                + "&grant_type=refresh_token&resource=https%3A%2F%2Fgraph.windows.net";
        String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
        byte[] data = params.getBytes(encoding);
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));
        conn.setConnectTimeout(5 * 1000);
        OutputStream outStream = conn.getOutputStream();
        outStream.write(data);
        outStream.flush();
        outStream.close();
        System.out.println(conn.getResponseCode());
        System.out.println(conn.getResponseMessage());

        BufferedReader br = null;
        if (conn.getResponseCode() != 200) {
            br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
        } else {
            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        }
        System.out.println("Response body : " + br.readLine());
    }

希望能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46483165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档