【发布时间】:2020-07-12 04:55:21
【问题描述】:
我有一个使用 Msal 库获得的 Azure AD JWT 令牌,但是当我尝试验证此令牌时出现问题:
客户端: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)
验证器:
<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());
}
我的问题是,当我尝试验证此令牌时,我收到此错误:使用算法验证时令牌的签名无效:SHA256withRSA
我被这个卡住了,如果令牌是正确的,为什么我会出现这个错误?
问候
【问题讨论】:
-
两个问题:1.你使用的是MSAL.js吗? 2.你是在MSAL获取令牌后手动验证令牌吗? MSAL 应该为您验证它,因此您不必自己进行任何显式验证。
-
嗨@Toby,感谢您的回复。是的,我正在使用 MSAL.js 在我的客户端中获取令牌。之后,我使用此令牌调用外部 API。当我尝试验证令牌时出现问题并失败。我已将我的验证器添加到代码中
-
您能否获得 JWT 令牌的副本并在 jwt.io 处对其进行解码?如果可以,那么您可以通过查看 jwt.io 的 HEADER 区域来验证令牌的签名是否使用 RSA256 算法进行签名。如果它不使用该算法,那么这就是问题所在。
-
如果令牌是使用 RSA256 算法签名的,那么您是否知道 Azure AD 是否使用默认设置来颁发令牌,或者您是否使用自定义签名算法设置了自定义配置和/或自定义证书?如果您使用的是自定义证书,请确保有关证书的以下内容: 1. 它没有过期。 2. 由公共机构发行。如果它是由私人机构颁发的,那么您将需要自定义代码来信任该颁发者。
-
嗨@Toby,是的“alg”:“RS256”,对吗?
标签: azure azure-active-directory