如何使用 public 键使用 vertx 验证 JWT 令牌
How to use public key to validate JWT Token using vertx
我正在将 Vertx 与 Kotlin 结合使用,并且想要对 JWT 令牌进行身份验证。
我在 One Login 的 Json Web 密钥文件中有 public 密钥。
{ "keys": [
{
"kty": "RSA",
"kid": "JRcO4nxs5jgc8YdN7I2hLO4V_ql1bdoiMXmcYgHm4Hs",
"n": "z8fZsz...Something..GHSTAoQw",
"e": "AQAB"
} ] }
如何在 Json Web 密钥文件中使用上面提到的 public 密钥来验证 JWT 令牌是否有效?
我用的class/method是JWTAuth.create(vertx, config)
var config = JWTAuthOptions(
pubSecKeys = listOf(PubSecKeyOptions(
algorithm = "RS256",
publicKey = "<Value Copied from above json's 'n' field>")))
var provider = JWTAuth.create(myvertx, config)
上面一行抛出一个异常说明
RuntimeException:java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:IOException:DerInputStream.getLength():lengthTag=71,太大。
我已经找到解决上述问题的方法。所以,如果你有一个 'certs' url which returns Json Web Key 文件,那么你可以从中创建一个 java.security.PublicKey 对象,它可以与 PubSecKeyOptions 一起使用对象。
您需要读取键 'n' 和 'e' 的值,'n' 称为模数,'e' 称为指数。现在,您可以获得一个有效的 JWTAuthOptions 对象,如下所示;
val n = Base64.getUrlDecoder().decode(nValue.toByteArray(StandardCharsets.UTF_8))
val e = Base64.getUrlDecoder().decode(eValue.toByteArray(StandardCharsets.UTF_8))
val thePublicKeyINeeded: PublicKey= =KeyFactory.getInstance("RSA).generatePublic(RSAPublicKeySpec(n, e))
val encodedBase64PublicKey:String = Base64.getEncoder().encodeToString(thePublicKeyINeeded.encoded)
var config = JWTAuthOptions(
pubSecKeys = listOf(PubSecKeyOptions(
algorithm = "RS256",
publicKey = encodedBase64PublicKey)))
我正在将 Vertx 与 Kotlin 结合使用,并且想要对 JWT 令牌进行身份验证。
我在 One Login 的 Json Web 密钥文件中有 public 密钥。
{ "keys": [
{
"kty": "RSA",
"kid": "JRcO4nxs5jgc8YdN7I2hLO4V_ql1bdoiMXmcYgHm4Hs",
"n": "z8fZsz...Something..GHSTAoQw",
"e": "AQAB"
} ] }
如何在 Json Web 密钥文件中使用上面提到的 public 密钥来验证 JWT 令牌是否有效?
我用的class/method是JWTAuth.create(vertx, config)
var config = JWTAuthOptions(
pubSecKeys = listOf(PubSecKeyOptions(
algorithm = "RS256",
publicKey = "<Value Copied from above json's 'n' field>")))
var provider = JWTAuth.create(myvertx, config)
上面一行抛出一个异常说明 RuntimeException:java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:IOException:DerInputStream.getLength():lengthTag=71,太大。
我已经找到解决上述问题的方法。所以,如果你有一个 'certs' url which returns Json Web Key 文件,那么你可以从中创建一个 java.security.PublicKey 对象,它可以与 PubSecKeyOptions 一起使用对象。
您需要读取键 'n' 和 'e' 的值,'n' 称为模数,'e' 称为指数。现在,您可以获得一个有效的 JWTAuthOptions 对象,如下所示;
val n = Base64.getUrlDecoder().decode(nValue.toByteArray(StandardCharsets.UTF_8))
val e = Base64.getUrlDecoder().decode(eValue.toByteArray(StandardCharsets.UTF_8))
val thePublicKeyINeeded: PublicKey= =KeyFactory.getInstance("RSA).generatePublic(RSAPublicKeySpec(n, e))
val encodedBase64PublicKey:String = Base64.getEncoder().encodeToString(thePublicKeyINeeded.encoded)
var config = JWTAuthOptions(
pubSecKeys = listOf(PubSecKeyOptions(
algorithm = "RS256",
publicKey = encodedBase64PublicKey)))