如何在 Laravel 中使用 JWT-auth 解码 JWT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48309645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to decode JWT using JWT-auth in laravel
提问by plu plu
Hello my issue is that I need to verify a JWT token coming from android and decode it to fetch the information in the payload but I can't seem to find a decode method in the JWT-Auth 0.5*, is there another way to decode the payload to get the data?
您好,我的问题是我需要验证来自 android 的 JWT 令牌并对其进行解码以获取有效负载中的信息,但我似乎无法在 JWT-Auth 0.5* 中找到解码方法,是否还有另一种解码方法获取数据的有效载荷?
回答by NanThiyagan
use Tymon\JWTAuth\Facades\JWTAuth; //use this library
try this
尝试这个
$token = JWTAuth::getToken();
$apy = JWTAuth::getPayload($token)->toArray();
also you can get other info like this
你也可以得到像这样的其他信息
try {
// attempt to verify the credentials and create a token for the user
$token = JWTAuth::getToken();
$apy = JWTAuth::getPayload($token)->toArray();
} catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['token_expired'], 500);
} catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['token_invalid'], 500);
} catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['token_absent' => $e->getMessage()], 500);
}