企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
 第一步:composer安装    ``` composer require firebase/php-jwt ``` ``` 第一步: composerrequire firebase/php-jwt 第二步 <?php namespace app\\controller; use Firebase\\JWT\\ExpiredException; use Firebase\\JWT\\JWT as JWTUtil; use think\\Exception; class JWT { /\*\* \* 根据json web token设置的规则生成token \* @return \\think\\response\\Json \*/ static public function createjwt() { $key = md5('dd'); //jwt的签发密钥,验证token的时候需要用到 $time = time(); //签发时间 $expire = $time + 14400; //过期时间 $token = array( "user\_id" => 1, "iss" => "http://www.najingquan.com/",//签发组织 "aud" => "zz", //签发作者 "iat" => $time, "nbf" => $time, "exp" => $expire ); return JWTUtil::encode($token,$key); } /\*\* \* 验证token \* @return \\think\\response\\Json \*/ static public function verifyjwt() { $jwt= input("jwt"); $key = md5('dd'); //jwt的签发密钥,验证token的时候需要用到 try { $jwtAuth = json\_encode(JWTUtil::decode($jwt, $key, array("HS256"))); $authInfo = json\_decode($jwtAuth, true); if (!$authInfo\['user\_id'\]) { return json(\[ 'msg'=>'失败', 'code'=>'600', 'data'=>'', \]); } return json(\[ 'msg'=>'OK', 'code'=>'200', 'data'=>'', \]); } catch (ExpiredException $e) { throw new Exception('token过期'); } catch (\\Exception $e) { throw new Exception($e->getMessage()); } } public static function getRequestToken() { if (empty($\_SERVER\['HTTP\_AUTHORIZATION'\])) { return false; } $header = $\_SERVER\['HTTP\_AUTHORIZATION'\]; $method = 'bearer'; //去除token中可能存在的bearer标识 return trim(str\_ireplace($method,'',$header)); } } ```