【问题标题】:Verify Signature with Azure AD使用 Azure AD 验证签名
【发布时间】: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 的支持?我的代码有什么问题?

在此先感谢

胡安·安东尼奥

【问题讨论】:

标签: azure spring-security azure-active-directory auth0 adfs


【解决方案1】:

您正在尝试验证用于 Microsoft Graph API 的访问令牌。 您不应该这样做,主要是因为它不适用于您的应用程序。 他们使用有点不同的东西进行签名,您不能使用相同的方法来验证 Graph API 令牌。

获取令牌时,请确保使用您的应用程序定义的范围来接收您的应用程序的令牌。 那个你应该能够验证。 如果还需要调用MS Graph API,则需要获取两个token。

【讨论】:

  • 是的,我会检查范围: curl --location --request POST 'login.microsoftonline.com{tenantid}/oauth2/v2.0/token' \ --header 'Content-Type : application/x-www-form-urlencoded' \ --data-urlencode 'client_id={clientId}' \ --data-urlencode 'client_secret={clientSecret}' \ --data-urlencode 'grant_type=client_credentials' \ - -data-urlencode 'scope=graph.microsoft.com/.default openid email'
  • 为应用程序添加自定义范围,我能够使用任何新的代码行来解决问题。谢谢@juunas,祝你有美好的一天。
猜你喜欢
  • 2017-11-22
  • 2017-06-20
  • 2017-05-19
  • 2018-09-13
  • 2017-09-02
  • 2018-09-30
  • 2021-08-10
  • 2018-12-20
  • 2020-05-29
相关资源
最近更新 更多