gpt4 book ai didi

azure - 如何生成 RSAPublicKey 以馈入 JJWT RSA token 验证

转载 作者:行者123 更新时间:2023-12-02 23:31:57 31 4
gpt4 key购买 nike

我正在验证来自 Azure 的 JWT token 并使用 JJWT。我从与我的 tid 相关的键文档中检索模数和指数,它们是字段分别为n和e。验证失败并出现错误:JWT 签名与本地计算的签名不匹配。 JWT 有效性无法断言,不应被信任。

这是代码。有人看到我犯的错误吗?代码在验证之前一直运行良好,在验证中抛出签名不匹配错误。

private Claims extractClaimsForRsaSignedJwts(String token, String mod, String exp) {
Claims claims = null;
byte[] modBytes = Base64.decodeBase64(mod.getBytes());
byte[] expBytes = Base64.decodeBase64(exp.getBytes());
BigInteger modulus = new BigInteger(modBytes);
BigInteger exponent = new BigInteger(expBytes);
RSAPublicKeySpec pubKeySpecification = new RSAPublicKeySpec(modulus, exponent);
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
RSAPublicKey rsaPub = null;
try {
rsaPub = (RSAPublicKey) keyFac.generatePublic(pubKeySpecification);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

JwtParser jwtParser = Jwts.parser().setSigningKey(rsaPub);

try {
claims = jwtParser.parseClaimsJws(token).getBody();
} catch (Exception e) {
// JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted.
System.out.println("The RSA JWT key validation failed: " + e.getMessage());
}

return claims;
}

谢谢!

一月

最佳答案

我发现问题了! BigInteger 应该用符号 1 来构造正数!现在,该代码就像 AzureAD JWT 签名验证的魅力一样。

    BigInteger modulus  = new BigInteger(1, modBytes);
BigInteger exponent = new BigInteger(1, expBytes);

这是最终的代码: Screen Shot Of Code With Correction

关于azure - 如何生成 RSAPublicKey 以馈入 JJWT RSA token 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44074961/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com