thinkphp5框架API token身份驗證功能示例
本文實例講述了thinkphp5框架API token身份驗證功能。分享給大家供大家參考,具體如下:
使用說明:登陸時生成token和刷新用的refresh_token,返回給客戶端,客戶端收到保存本地localStorage等,每次訪問接口帶上token,后端驗證token存在并且一致后方可執(zhí)行接下來的動作,假如不存在就返回token過期,客戶端調(diào)用刷新接口傳入token和refresh_token,服務(wù)器端進行驗證,驗證通過重新生成新的token保存數(shù)據(jù)庫,返回給客戶端客戶端刷新本地token訪問即可繼續(xù),當(dāng)refresh_token驗證失敗就清除數(shù)據(jù)庫token,過期時間等信息
簡單的token生成函數(shù)(公共函數(shù)文件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);//設(shè)置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 = '參數(shù)錯誤'; 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'); }
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
- Thinkphp 在api開發(fā)中異常返回依然是html的解決方式
- ThinkPHP框架整合微信支付之JSAPI模式圖文詳解
- ThinkPHP實現(xiàn)微信支付(jsapi支付)流程教程詳解
- thinkPHP5.0框架API優(yōu)化后的友好性分析
- Thinkphp5框架ajax接口實現(xiàn)方法分析
- thinkPHP5框架接口寫法簡單示例
- ThinkPHP框架實現(xiàn)的微信支付接口開發(fā)完整示例
- thinkPHP框架實現(xiàn)的短信接口驗證碼功能示例
- thinkPHP微信分享接口JSSDK用法實例
- thinkPHP框架對接支付寶即時到賬接口回調(diào)操作示例
- ThinkPHP和UCenter接口沖突的解決方法
- thinkphp使用url請求調(diào)用ThinkApi天氣教程【圖文詳解】
相關(guān)文章
php中使用cookie來保存用戶登錄信息的實現(xiàn)代碼
php中使用cookie來保存用戶登錄信息的實現(xiàn)代碼,使用php開發(fā)的朋友可以參考下2012-03-03詳解php中生成標(biāo)準(zhǔn)uuid(guid)的方法
這篇文章主要介紹了php中生成標(biāo)準(zhǔn)uuid(guid)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Yii2 ActiveRecord多表關(guān)聯(lián)及多表關(guān)聯(lián)搜索的實現(xiàn)
這篇文章主要介紹了Yii2 ActiveRecord多表關(guān)聯(lián)及多表關(guān)聯(lián)搜索的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-06-06PHP實現(xiàn)四種基礎(chǔ)排序算法的運行時間比較(推薦)
本文給大家介紹PHP實現(xiàn)四種基礎(chǔ)排序算法的運行時間比較,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-08-08php while循環(huán)得到循環(huán)次數(shù)
在for循環(huán)中,我們很容易得到循環(huán)次數(shù),因為是作為條件出現(xiàn)的。在while也可以得到,如下:2013-10-10PHP--用萬網(wǎng)的接口實現(xiàn)域名查詢功能
PHP用萬網(wǎng)的接口實現(xiàn)域名查詢功能,需要的朋友可以了解下2012-12-12