您当前的位置: 首页 >  中间件

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

tp6token进行合法性验证(中间件)

发布时间:2022-05-24 13:13:08 ,浏览量:0

第一个composer:下载jwt插件 第二个composer:必须降级jwt才能使用

composer require lcobucci/jwt
composer require lcobucci/jwt=3.3.3 

在这里插入图片描述

 /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */ public function handle($request, \Closure $next) { $header = $request->header();//获取jwt里面的header函数.因为header里面会生成token if(!isset($header['token'])){//没有token的话,进行if里面. return json(['code'=>440,'msg'=>'request must with token']); } $token = $header['token']; try{ $token = (new Parser())->parse($token);//token解析,解析成一个对象(切记,如果用户随意改的token会进入catch里面) }catch(\Exception $e){ return json(['code'=>440,'msg'=>'invalid token']);//随意改的token会进入里面 } $signer = new Sha256();//创建加密对象 //verify进行合法性验证 if(!$token->verify($signer,config('shop.API_KEY'))){ return json(['code'=>440,'msg'=>'token verify failed']); } $data = new ValidationData(); //验证token是否在有效期内  if(!$token->validate($data)){//如果验证不通过的话。(token不再有效期内) $mobile = $token->getClaim('mobile'); $token = getToken($mobile); return json(['code'=>450,'msg'=>'token expired','token'=>$token]); } return $next($request); } } 
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.8658s