thinkphp5框架API token身分驗證功能【範例】
以下由thinkphp教學專欄跟大家介紹【範例】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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

寶塔部署thinkphp5報錯的解決方法:1、開啟寶塔伺服器,安裝php pathinfo擴充並啟用;2、設定「.access」文件,內容為「RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]」;3、在網站管理裡面,啟用thinkphp的偽靜態即可。

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標題列icon的方法:1、找到thinkphp5框架public下的favicon.ico檔案;2、刪除該檔案或選擇另一張圖片命名改為favicon.ico,並取代原favicon.ico檔案即可。

thinkphp5取得請求網址的方法:1.使用「\think\Request」類別的「$request = Request::instance();」方法取得目前的url資訊;2、透過自帶的助手函數「$request-> url()」取得包含網域的完整URL位址。

thinkphp5 post無法得到值是因為TP5是透過strpos函數在Header的content-type值中找出app/json字串的,其解決辦法就是設定Header的content-type值為app/json即可。

thinkphp5提示控制器不存在的解決方法:1、檢查對應的控制器裡面的命名空間是否寫對,修改為正確的命名空間;2、打開對應的tp文件,修改類別名稱即可。

ThinkPHP5查詢昨天資料的方法:1、開啟ThinkPHP5相關檔案;2、透過表達式「db('table')->whereTime('c_time', 'yesterday')->select();」查詢昨天的資料即可。

thinkphp5設定報錯提示的方法:1、進入專案根目錄下的public資料夾,開啟index.php入口檔案;2、檢視偵錯模式開關的註解;3、將「APP_DEBUG」常數的值調整為true即可展示錯誤訊息提示。
