gpt4 book ai didi

java - 如何使用 nimbus-jose-jwt 解码 JWT token 以获取 header 和有效负载的详细信息?

转载 作者:行者123 更新时间:2023-12-02 01:09:54 33 4
gpt4 key购买 nike

我想要解码以下 JWT token :

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5UQXhabU14TkRNeVpEZzNNVFUxWkdNME16RXpPREpoWldJNE5ETmxaRFUxT0dGa05qRmlNUSJ9.eyJhdWQiOiJodHRwOlwvXC9vcmcud3NvMi5hcGltZ3RcL2dhdGV3YXkiLCJzdWIiOiJTYW1wYXRoIiwiYXBwbGljYXRpb24iOnsiaWQiOjMsIm5hbWUiOiJUZXN0IEpXVCIsInRpZXIiOiJVbmxpbWl0ZWQiLCJvd25lciI6IlNhbXBhdGgifSwic2NvcGUiOiJhbV9hcHBsaWNhdGlvbl9zY29wZSBkZWZhdWx0IiwiaXNzIjoiaHR0cHM6XC9cL2xvY2FsaG9zdDo5NDQzXC9vYXV0aDJcL3Rva2VuIiwia2V5dHlwZSI6IlBST0RVQ1RJT04iLCJzdWJzY3JpYmVkQVBJcyI6W10sImNvbnN1bWVyS2V5IjoiRlBHa2dKNndQMGZWMUtNS3FkRW83X1U1ZFNFYSIsImV4cCI6MTU0OTU0MjM0NSwiaWF0IjoxNTQ5NTM4NzQ1Mjc4LCJqdGkiOiJjMDU4ODMxYS03Yzg0LTQwZGEtYTFkOC04NWRiNGE2YzY4ZDMifQ==.g3CZ6PfirbN9rUHDhWxXNbqA49ne2Fam_SXWiKUBfQN10ddLIKrY8L8xalQ0DX3LiQg0TmdYspR4vVd3Mfji919UJEFSe894JD-PqFTFcvVsLgLm9pWNoyXNVEz6gTiR5JM_UlAPZ4N8FjCla9tMjRfiG-l4gvdmXhF84_a7IwwVLY7DkOs0m-7UK3evB005QZJ5_vmYEtAFV6pFzSt3Jz_fqEPR4RIkrjUIei7m4KyQ3QEl-_ZuhPn80UrACbFqyIQ0Fl71cMQ5V28AXKt8AitGT53XL0LK-cnNmQqyy8WTNRaWmVAZIW7XOCcHBkg9diWy-uZvHSL3pig9Pfr0sg==

如果我这样做online ,我得到以下响应:

// HEADER
{
"typ": "JWT",
"alg": "RS256",
"x5t": "NTAxZmMxNDMyZDg3MTU1ZGM0MzEzODJhZWI4NDNlZDU1OGFkNjFiMQ"
},
// PAYLOAD
{
"aud": "http://org.wso2.apimgt/gateway",
"sub": "Sampath",
"application": {
"id": 3,
"name": "Test JWT",
"tier": "Unlimited",
"owner": "Sampath"
},
"scope": "am_application_scope default",
"iss": "https://localhost:9443/oauth2/token",
"keytype": "PRODUCTION",
"subscribedAPIs": [],
"consumerKey": "FPGkgJ6wP0fV1KMKqdEo7_U5dSEa",
"exp": 1549542345,
"iat": 1549538745278,
"jti": "c058831a-7c84-40da-a1d8-85db4a6c68d3"
}

如何使用 Java 代码和 Nimbus 来完成此操作?

最佳答案

使用 Nimbus:https://connect2id.com/products/nimbus-jose-jwt/examples/jose-jwt-parsing

pom.xml

<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>9.x</version>
</dependency>

yourClass.java

/**
* accessToken: the JWT string text.
**/
private String parseJWT(String accessToken) {
try {
var decodedJWT = SignedJWT.parse(accessToken);
var header = decodedJWT.getHeader().toString();
var payload = decodedJWT.getPayload().toString();
} catch (ParseException e) {
throw new Exception("Invalid token!");
}
}

您还可以将 SignedJWT 更改为 EncryptedJWTPlainJWT,具体取决于您拥有的 token 类型(示例中的 token )是一个签名的)。

关于java - 如何使用 nimbus-jose-jwt 解码 JWT token 以获取 header 和有效负载的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696416/

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