thinkphp5拦截验证token

写一个BaseController 类

基本思路:

1、继承一个公共基类,将验证相关代码放在基类

2、根据 孩子类下的notNeedToken 来决定是否进行验证

3、验证失败后,直接响应回来

这里需要封装一个主要代码:

php 复制代码
 protected function response($data = [])
  {

    $type = $this->getResponseType();
    $response = Response::create($data, $type);
    throw new HttpResponseException($response);
  }

如果直接return 返回,是不会终止执行的,而是会继续执行到指定的控制器,所以这里的方法仿造了this-\>error() 以及 this->success()方法。

全部代码

php 复制代码
<?php

namespace app\api\controller;

use think\Controller;
use think\exception\HttpResponseException;
use think\Request;
use think\Response;

class BaseController extends Controller
{
  protected $tokenError = ''; // 错误信息
  protected $tokenInfo = null; // 解析出来的token信息
  protected $statusCode = 200;

  protected $notNeedToken = [];

  public function _initialize()
  {
    $this->checkToken();
    $this->statusCode = $this->tokenError == '' ? 200 : 401;
    if (!empty($this->tokenError)) {
      $this->response([
        'code' => 0,
        'msg' => $this->tokenError
      ]);
    }
  }

  // 检查token
  public function checkToken()
  {

    $request = Request::instance();
    $path = $request->path();
    $tokenStr = $request->header('Authorization');

    do {
      //echo '所有路由都需要执行' . $path;
      // 不在排除的路由列表当中都需要进行验证
      if (!in_array($path, $this->notNeedToken)) {
        if (empty($tokenStr)) {
          // 没有传递token
          $this->tokenError = '请先登录后操作';
          break;
        }

        [$a, $token] = explode(" ", $tokenStr);

        // 如果为空 直接返回错误
        if (empty($token)) {
          $this->tokenError = '请先登录后操作';
          break;
        }
        // 进入验证操作
        $rst = checkToken($token);
        if ($rst['code'] == 2) {
          $this->tokenError = $rst['msg'];
          break;
        }

        // token本身还没有过期
        $this->tokenInfo = $rst['data'];


        // 检查数据库表当中是否已经标记为过期了(可能执行了退出登录了)
        $appUserToken = model('api/app_user_token');
        $rst =  $appUserToken::get([
          'user_id' =>   $this->tokenInfo->id,
          'token' =>   $token
        ]);


        // 不存在该信息
        if (empty($rst)) {
          $this->tokenError = '没有该登录信息';
          break;
        }

        if ($rst['is_overtime'] == 1) {
          $this->tokenError = '登录已过期';
          break;
        }
      }
    } while (0);
  }

  protected function response($data = [])
  {

    $type = $this->getResponseType();
    $response = Response::create($data, $type);
    throw new HttpResponseException($response);
  }
}

控制器UserController

php 复制代码
class User extends BaseController
{
    //不需要验证token的接口
    protected $notNeedToken = [
        'api/user/reg',
        'api/user/login'
    ];
}
相关推荐
mit6.8241 小时前
[身份验证脚手架] 应用布局如何构建
架构·php·后端框架
BingoGo4 小时前
PHP 内存管理 深入理解 PHP 的引用和垃圾回收
后端·php
As33100104 小时前
IDM 下载失败排查指南:全面解析与解决方案
开发语言·php·idm
林深时见鹿74919 小时前
使用k8s k3s kuboard 部署 php hyperf 框架
php
长城202419 小时前
从词源和输出生成等角度详细解析PHP中常用文件操作类函数
php·文件·函数·文件操作函数
长城202420 小时前
PHP如何使用JpGraph生成3D饼形图?
开发语言·php·jpgraph·3d饼形图
熬夜苦读学习1 天前
Reactor 反应堆模式
运维·服务器·网络·网络协议·http·智能路由器·php
小森林81 天前
分享一次Guzzlehttp上传批量图片优化的经历
后端·php
THMAIL1 天前
大模型0基础开发入门与实践:第11章 进阶:LangChain与外部工具调用
开发语言·langchain·php
分享点2 天前
Laravel 使用阿里云OSS S3 协议文件上传
阿里云·php·laravel