# JWT插件
composer 安裝
~~~
composer require firebase/php-jwt
~~~
# 用法
~~~
use \Firebase\JWT\JWT;
$key = "example_key";
$token = array(
//"iss" => "http://example.org",
// "aud" => "http://example.com",
"iat" => 1356999524,//可以是time()時間戳
"nbf" => 1357000000//可以是time()時間戳
);
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
print_r($decoded);
$decoded_array = (array) $decoded;
JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode($jwt, $key, array('HS256'));
~~~
## 補充捕獲異常
~~~
try {
$arr = $jwt::decode($token, $key, array('HS256'));
} catch (\Exception $e) { // token驗證失敗
return json(['code' => 0, 'msg' => $e->getMessage()],400)->send();
}catch (ExpiredException $e){ // 過期
return json(['code' => 0, 'msg' => $e->getMessage()],400)->send();
}
~~~
## 實例
~~~
class Base extends Common
{
public function index(Request $request){
$data['username'] = $request->param('username');
//驗證數據
//查詢數據庫
$info = SystemUser::where('id','=','1')->find();
//比對密碼
//返回token
$key = 'asdfhjkl';
$payload=[
'iat'=>time(),
'nbf'=>time(),
'userinfo'=>[
'uid'=>$info['id'],
'name'=>$info['username'],
]
];
$jwt = JWT::encode($payload,$key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
return json(['code'=>'1','token'=>$decoded,'msg'=>'成功'])->send();
}
}
~~~
- tp5圖片上傳
- 文件上傳到七牛云
- 上傳到阿里云
- 富文本編輯器
- phpexcel和spreadsheet
- phpexcel導出
- phpexcel導入
- spreadsheet
- tp5_api接口
- 跨域請求
- JWT
- 圖片和視頻上傳接口
- 驗證碼
- tp5小程序登錄
- tp5小程序支付
- tp5基礎架構
- 驗證層
- 模型層Model
- 控制器構找
- tp5.0支付寶
- 海報二維碼
- 輪播圖
- echarts柱狀圖
- layui的圖片彈窗
- p標簽顯示指定行數(全部)
- jquery和layerdate調用日期
- ajax發送文件和圖片的坑啊
- JS日期點擊上一天和下一天
- 百度分享js
- POST請求
- 商品數據表
- tp5.0支付寶最全
- tp5路由的坑
- 二維數組排序
- tp5模型分組group錯誤
- 二維變一維數組
- 無限樹形結構
- json對象轉數組
- 模型關聯查詢
- tp5的模型獲取器和字段設定
- 經緯度獲取距離排序