验证Java上的AWS ID令牌

我在亚马逊使用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步。

据我所见,我从字符串亚马逊得到的模数和指数,我必须与这些,以验证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);} 

希望对有同样问题的人有所帮助。

Interesting Posts