发布于 2016-08-04 02:22:29
你应该拆分字符串:如果你通过一个base64解码器传递前两个部分,你将得到以下内容(为清晰起见增加了格式):
标题
{
"alg": "HS256",
"typ": "JWT"
}
正文
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
代码示例:
public class JWTUtils {
public static void decoded(String JWTEncoded) throws Exception {
try {
String[] split = JWTEncoded.split("\\.");
Log.d("JWT_DECODED", "Header: " + getJson(split[0]));
Log.d("JWT_DECODED", "Body: " + getJson(split[1]));
} catch (UnsupportedEncodingException e) {
//Error
}
}
private static String getJson(String strEncoded) throws UnsupportedEncodingException{
byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
return new String(decodedBytes, "UTF-8");
}
}
调用方法,例如
JWTUtils.decoded("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ");
图书馆参考:https://github.com/jwtk/jjwt
jwt测试:https://jwt.io/
发布于 2017-03-16 21:30:51
我使用了一个名为JWTDecode.Android https://github.com/auth0/JWTDecode.Android的第三方库。文档相当不错。从你的问题看,潜艇、名字等都是身体的一部分,被称为索赔。您可以使用上面的库来获取它们:
JWT parsedJWT = new JWT(jwtToken);
Claim subscriptionMetaData = parsedJWT.getClaim("name");
String parsedValue = subscriptionMetaData.asString();
发布于 2017-05-16 03:24:03
这是使用Java8的Base64类实现的:
public String getDecodedJwt(String jwt)
{
String result = "";
String[] parts = jwt.split("[.]");
try
{
int index = 0;
for(String part: parts)
{
if (index >= 2)
break;
index++;
byte[] partAsBytes = part.getBytes("UTF-8");
String decodedPart = new String(java.util.Base64.getUrlDecoder().decode(partAsBytes), "UTF-8");
result += decodedPart;
}
}
catch(Exception e)
{
throw new RuntimeException("Couldnt decode jwt", e);
}
return result;
}
https://stackoverflow.com/questions/37695877
复制相似问题