重新获取访问令牌并解析正确的访问令牌。
代码示例:
// 在用于获取访问令牌的方法中添加以下代码 // 验证并更新访问令牌 AuthenticationResult result = adapter.acquireTokenSilentSync(resource, clientID, userIdentifier);
if (result == null) { result = adapter.acquireToken(resource, clientID, redirectUri, promptBehavior, null, null, Constants.OAUTH2_SCOPE); } else { String accessToken = result.getAccessToken(); if (!StringUtils.isNullOrEmpty(accessToken)) { JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); JwtSecurityToken jwt = handler.ReadJwtToken(accessToken); Date expirationTime = jwt.ValidTo; Date now = new Date(); if (now.after(expirationTime)) { result = adapter.acquireToken(resource, clientID, redirectUri, promptBehavior, null, null, Constants.OAUTH2_SCOPE); } } }
// 解析正确的访问令牌 String accessToken = result.getAccessToken(); JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); JwtSecurityToken jwt = handler.ReadJwtToken(accessToken);