我想在移动应用中解码JWT令牌。 我遵循了这种方法。 https://stackoverflow.com/a/52021206/11065582 但输出是
{"site_id":"2","isActualNSolutionAvailable":"True","isCauseNEffectsAvailable":"True","isServiceCenterAvailable":"True","user_id":"190","employee_id":"36","user_name":"...","userPassword":"o+2r8b/9k9i75j4VE+9xNmJnQpbipT1lYRzzrciAXqWkE/jNzg6Qk5MWP16A==","userPWDSalt":"lbNmVWQGMsvlkhuX/YN6P6mobkC0L+1fgFtL6ttN3KI3/kXH/+OtBPff1A+WGfltiOmXf3Nbs2ewItPHhkrmHask+++UNNxSGm+xD/7Dz8vCxOzRTP5OE/Kf3LFXZwq1UmafhxIIYVua7m8eApulR+HwW47/RUKfJZvIZYjE80AYkoetYJmx4hdu+NBxItv1fyx1TGpMKbB+vY9F6CJK9xp76yrjtnzlctgJb5ID/xx/TMj9qLaBx+pAHGc3BZYRdlCXJYnHEasHTtDk2RoYvnv24iQpS5Q==","exp":1551332124,"iss":"sl.com","aud":"sl.com"}
如何仅获取 site_id ?
答案 0 :(得分:0)
根据您添加的方式执行以下操作(检查_decodeJWT
方法):
Map<String, dynamic> parseJwt(String token) {
final parts = token.split('.');
if (parts.length != 3) {
throw Exception('invalid token');
}
final payload = _decodeBase64(parts[1]);
final payloadMap = json.decode(payload);
if (payloadMap is! Map<String, dynamic>) {
throw Exception('invalid payload');
}
return payloadMap;
}
_decodeJwt(String token){
Map<String,dynamic> tokenDecoded = parseJWT(token);
String siteId = tokenDecoded['site_id'];
}