xii*_*vil 3 java rsa amazon-web-services jwt amazon-cognito
我在 Amazon 中使用 Cognito 对我的移动用户进行身份验证,一旦他们完成登录,Cognito 会提供一组令牌,我在后端使用 id 令牌。我已按照https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with上的在 Web API 中使用 ID 令牌和访问令牌部分中的步骤进行操作 -identity-providers.html我被困在第 6 步。
据我所知,我从 Amazon 获得了字符串中的模数和指数,我必须用它们构建一个 PublicKey,以验证 JWT 签名。
我不知道如何使用 String 中的这两个参数构建 PublicKey。
我终于找到了解决方法,aws 论坛中有一个示例https://forums.aws.amazon.com/message.jspa?messageID=728870,但代码在 Kotlin 中。我只是将它移植到 java 并进行了一些测试,我最终验证了我的 JWT 签名:
byte[] decodedModulus = Base64.getUrlDecoder().decode(yourModulus);
byte[] decodedExponent = Base64.getUrlDecoder().decode(yourExponent);
BigInteger modulus = new BigInteger(1, decodedModulus);
BigInteger exponent = new BigInteger(1, decodedExponent);
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
JWSVerifier verifier = new RSASSAVerifier((RSAPublicKey) publicKey);
Boolean verify = parsedToken.verify(verifier);}
Run Code Online (Sandbox Code Playgroud)
希望对遇到同样问题的人有所帮助。
| 归档时间: |
|
| 查看次数: |
3489 次 |
| 最近记录: |