搜尋
首頁php框架ThinkPHPthinkphp5框架API token身分驗證功能【範例】

以下由thinkphp教學專欄跟大家介紹【範例】thinkphp5框架API token身分驗證功能,希望對需要的朋友有幫助!

thinkphp5框架API token身分驗證功能【範例】

使用說明:登陸時產生token和刷新用的refresh_token,返回給客戶端,客戶端收到保存本地localStorage等,每次訪問接口帶上token,後端驗證token存在且一致後方可執行接下來的動作,假如不存在就返回token過期,客戶端呼叫刷新介面傳入token和refresh_token,伺服器端進行驗證,驗證透過重新產生新的token保存資料庫,返回給予客戶端用戶端刷新本地token存取即可繼續,當refresh_token驗證失敗就清除資料庫token,過期時間等資訊

簡單的token產生函數(公用函數檔common)

function create_token($id,$out_time){
  return substr(md5($id.$out_time),5,26);
}

驗證登陸方法(模型)

public function checkLogin($username,$passwd){
    $driver = self::field('driver_id,passwd')->where('zhanghao',$username)->whereOr('phone',$username)->find();
    if (empty($driver)){
      $this->error = '账号不存在';
      return false;
    }
    if ($driver['passwd'] != md5($passwd)){
      $this->error = "密码不正确";
      return false;
    }
    //$out_time = strtotime('+ 1 days');
    $out_time = strtotime('+ 1 minutes');
    $token = create_token($driver['driver_id'],$out_time);
    if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){
      $this->error = '登陆失败';
      return false;
    }
    $refresh_token_out_time = strtotime('+ 5 days');
    $refresh_token = create_token($driver['driver_id'],$refresh_token_out_time);
    Cache::set("token",$token,60);
    Cache::set("driver_id",$driver['driver_id'],$refresh_token_out_time);//设置ID的过期时间和更新token的token时间一样用于更新的时候获取用户信息
    Cache::set('refresh_token',$refresh_token,$refresh_token_out_time);
    return ['token'=>$token,'refresh_token'=>$refresh_token,'in_expire'=>$out_time];
}

token刷新方法(模型)

public function refreshToken($refresh_token,$token){
    if (!isset(Cache::get('refresh_token')) or Cache::get('refresh_token')!=$refresh_token){
      $this->error = '刷新token失败';
      return false;
    }
    $cache_driver_id = Cache::get('driver_id');
    $driver = self::field('driver_id,passwd')->where('driver_id',$cache_driver_id)->where('token',$token)->find();
    if (empty($driver)){
      $this->error = '参数错误';
      return false;
    }
    $out_time = strtotime('+ 1 days');//新的过期时间
    $token = create_token($driver['driver_id'],$out_time);//更新token
    if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){
      Cache::clear($token);
      $this->error = '刷新失败';
      return false;
    }
    Cache::set("token",$token,864000);
    return ['token'=>$token,'in_expire'=>$out_time];
}

#退出方法(模型)

public function logout($token,$refresh_token=''){
    $driver = self::field('driver_id,passwd')->where('token',$token)->find();
    self::save(['token'=>'','time_out'=>''],['token'=>$token]);
    Cache::clear('token');
    Cache::clear('refresh_token');
}

以上是thinkphp5框架API token身分驗證功能【範例】的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:jb51。如有侵權,請聯絡admin@php.cn刪除
宝塔部署thinkphp5报错怎么办宝塔部署thinkphp5报错怎么办Dec 19, 2022 am 11:04 AM

宝塔部署thinkphp5报错的解决办法:1、打开宝塔服务器,安装php pathinfo扩展并启用;2、配置“.access”文件,内容为“RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]”;3、在网站管理里面,启用thinkphp的伪静态即可。

thinkphp5 url重写不行怎么办thinkphp5 url重写不行怎么办Dec 12, 2022 am 09:31 AM

thinkphp5 url重写不行的解决办法:1、查看httpd.conf配置文件中是否加载了mod_rewrite.so模块;2、将AllowOverride None中的None改为All;3、修改Apache配置文件.htaccess为“RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]”保存即可。

thinkphp5 post得不到值怎么办thinkphp5 post得不到值怎么办Dec 06, 2022 am 09:29 AM

thinkphp5 post得不到值是因为TP5是通过strpos函数在Header的content-type值中查找app/json字符串的,其解决办法就是设置Header的content-type值为app/json即可。

thinkphp5怎么获取请求过来的网址thinkphp5怎么获取请求过来的网址Dec 20, 2022 am 09:48 AM

thinkphp5获取请求网址的方法:1、使用“\think\Request”类的“$request = Request::instance();”方法获取当前的url信息;2、通过自带的助手函数“$request->url()”获取包含域名的完整URL地址。

怎么去除thinkphp5标题栏icon怎么去除thinkphp5标题栏iconDec 20, 2022 am 09:24 AM

去除thinkphp5标题栏icon的方法:1、找到thinkphp5框架public下的favicon.ico文件;2、删除该文件或者选择另一张图片命名改为favicon.ico,并替换原favicon.ico文件即可。

thinkphp5提示控制器不存在怎么办thinkphp5提示控制器不存在怎么办Dec 06, 2022 am 10:43 AM

thinkphp5提示控制器不存在的解决办法:1、检查对应的控制器里面的命名空间是否写对,修改为正确的命名空间;2、打开相应的tp文件,修改类名即可。

thinkphp5报错提示怎么设置thinkphp5报错提示怎么设置Dec 07, 2022 am 10:31 AM

thinkphp5设置报错提示的方法:1、进入项目根目录下的public文件夹,打开index.php入口文件;2、查看调试模式开关的注释;3、将“APP_DEBUG”常量的值调整为true即可展示错误信息提示。

ThinkPHP5怎么查询昨天的数据ThinkPHP5怎么查询昨天的数据Dec 05, 2022 am 09:20 AM

ThinkPHP5查询昨天数据的方法:1、打开ThinkPHP5相关文件;2、通过表达式“db('table')->whereTime('c_time', 'yesterday')->select();”查询昨天的数据即可。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版