【发布时间】:2021-09-21 19:33:21
【问题描述】:
我正在尝试使用 Java 在 Azure AD 中验证签名:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.6.6</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.18.2</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>jwks-rsa</artifactId>
<version>0.19.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.5</version>
</dependency>
如果我尝试验证令牌,则会收到以下错误:
使用 算法:SHA256withRSA
应用以下方法:
private boolean verifyJWT(String azureDiscoveryKeys, String token) {
try {
JwkProvider provider = new UrlJwkProvider(new URL(azureDiscoveryKeys));
DecodedJWT jwt = JWT.decode(token);
Jwk jwk = provider.get(jwt.getKeyId());
RSAPublicKey publicKey = (RSAPublicKey) jwk.getPublicKey();
Algorithm alg = Algorithm.RSA256(publicKey, null);
JWTVerifier verifier = JWT.require(alg).build();
verifier.verify(token);
return true;
} catch(JWTVerificationException | JwkException | MalformedURLException ex) {
System.out.println(ex.getMessage());
return false;
}
}
使用 MS ADFS 提供的令牌测试相同的方法,我能够验证,但使用 Azure AD,我无法验证。如何验证令牌以避免此错误?如何调整 Azure AD 的代码或添加对 SHA256withRSA 的支持?我的代码有什么问题?
在此先感谢
胡安·安东尼奥
【问题讨论】:
-
你从哪里得到公钥?您使用什么 URL 来获取令牌?
-
嗨,我使用了 3 个不同的位置: String address = "login.microsoftonline.com/common/discovery/v2.0/keys";字符串地址 = "login.microsoftonline.com{tenantid}/discovery/keys";字符串地址 = "login.microsoftonline.com/common/discovery/keys";
-
但是我收到了同样的结果
-
好的,我现在想知道您是否正在尝试验证 MS Graph API 令牌 :) 如果您在例如解码令牌中的 aud 声明(受众)是什么? jwt.ms?
-
"aud": "graph.microsoft.com",
标签: azure spring-security azure-active-directory auth0 adfs