【问题标题】:Access token signature validation failing while invoking Microsoft Identity Platform调用 Microsoft Identity Platform 时访问令牌签名验证失败
【发布时间】:2021-07-30 23:14:31
【问题描述】:

我正在使用 auth0 并尝试通过调用 Microsoft 标识平台来验证签名,但在调用验证方法时收到以下错误。也请找到我的代码 sn-p。

代码片段:

  try {
        DecodedJWT decodedJWT = JWT.decode(accessToken); // your string
        JwkProvider provider =  new JwkProviderBuilder(new URL("https://login.microsoftonline.com/tid/discovery/v2.0/keys")).build();
        Jwk jwk = provider.get(decodedJWT.getKeyId());
        Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
        JWTVerifier verifier = JWT.require(algorithm)
                                  .withIssuer("auth0")
                                  .build();
       
        
        verifier.verify(decodedJWT);
        

    } catch (JWTVerificationException | JwkException | MalformedURLException e) {
        e.printStackTrace();
    }

错误

com.auth0.jwt.exceptions.SignatureVerificationException:使用算法验证时,令牌的签名无效:SHA256withRSA 在 com.auth0.jwt.algorithms.RSAAlgorithm.verify(RSAAlgorithm.java:50) 在 com.auth0.jwt.JWTVerifier.verify(JWTVerifier.java:299) 在 com.identity.telsmsidentity.util.MSTokenValidator.validateMSToken(MSTokenValidator.java:43) 在 com.identity.telsmsidentity.controller.TelsPageController.validateToken(TelsPageController.java:98) 在 com.identity.telsmsidentity.controller.TelsPageController.getUserInfoFromGraph(TelsPageController.java:59) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:564) 在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

【问题讨论】:

  • 您的令牌是由 Auth0 还是由 Azure AD 颁发的?
  • 此令牌由 Azure AD 颁发,但即使我将颁发者提供为 Azure AD,也会出现相同的错误。不确定我提供的信息是否正确。

标签: java azure-active-directory jwt auth0 microsoft-identity-platform


【解决方案1】:

以下代码适用于我,请确保您的令牌版本并使用相应的版本:

String token = "";
String tenantID = "";
// For Azure AD V1 token
String providerURLV1 = "https://login.microsoftonline.com/" + tenantID + "/discovery/keys";
String issuerV1 = "https://sts.windows.net/" + tenantID + "/";
// For Azure AD V2 token
String providerURLV2 = "https://login.microsoftonline.com/" + tenantID + "/discovery/v2.0/keys";
String issuerV2 = "https://login.microsoftonline.com/" + tenantID + "/v2.0";

try {
    DecodedJWT decodedJWT = JWT.decode(token);
    JwkProvider provider = new JwkProviderBuilder(new URL(providerURLV1)).build();
    Jwk jwk = provider.get(decodedJWT.getKeyId());
    Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
    JWTVerifier verifier = JWT.require(algorithm).withIssuer(issuerV1).build();

    System.out.println(verifier.verify(decodedJWT).getClaims());

} catch (Exception e) {
    e.printStackTrace();
}

结果:

【讨论】:

  • @Aryansh 很高兴为您提供帮助:)
  • 嗨@Stanley,我很抱歉,仍然遇到同样的错误。请注意,我使用的是 V1 版本。无法弄清楚出了什么问题。
  • @ Stanley ,我注意到无法从浏览器访问颁发者 URL 的一件事。
  • 这是设计使然,发行者 URL 不用于从浏览器访问。您能否通过catch (Exception e) { e.printStackTrace(); } 提供更多错误详细信息?
  • com.auth0.jwt.exceptions.SignatureVerificationException: The Token's Signature resulted invalid when verified using the Algorithm: SHA256withRSA at com.auth0.jwt.algorithms.RSAAlgorithm.verify(RSAAlgorithm.java:50) at com.auth0.jwt.JWTVerifier.verify(JWTVerifier.java:299) at com.identity.telsmsidentity.util.MSTokenValidator.validateSignature(MSTokenValidator.java:41) at com.identity.telsmsidentity.util.MSTokenValidator.validateMSToken(MSTokenValidator.java:21) at com.identity.telsmsidentity.controller.TelsPageController.getUserInfoFromGraph
猜你喜欢
  • 2017-11-26
  • 2016-10-18
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多