composer安装 composer require firebase/php-jwt <?php namespace app\busines; use Firebase\JWT\ExpiredException; use Firebase\JWT\JWT as JWTUtil; class Jwt { public function createjwt($id) { $key = md5(env('TOKEN.key', 'yxw')); //jwt的签发密钥,验证token的时候需要用到 $time = time(); //签发时间 $expire = $time + 14400; //过期时间 $token = array( "user_id" => $id, "iss" => env('TOKEN.iss'),//签发组织 "aud" => env('TOKEN.aud'), //签发作者` "iat" => $time, "nbf" => $time, "exp" => $expire ); $jwt = JWTUtil::encode($token, $key); return success('生成秘钥', $jwt); } public function verifyjwt($jwt) { // $jwt = input("jwt"); $key = md5(env('TOKEN.key', 'yxw')); //jwt的签发密钥,验证token的时候需要用到 try { $jwtAuth = json_encode(JWTUtil::decode($jwt, $key, array("HS256"))); $authInfo = json_decode($jwtAuth, true); if (!$authInfo['user_id']) { return fail('未找到用户'); } return success('ok'); } catch (ExpiredException $e) { return fail('token过期'); } catch (\Exception $e) { return fail('错误信息', $e->getMessage()); } } }
Thinkphp6封装jwt认证
最新推荐文章于 2021-12-30 09:38:03 发布