### 第一種 引用第三方
`composer require firebase/php-jwt`
~~~
<?php
namespace app\service;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class JwtService
{
/**
* 創建token
* @param $login_id
* @param $login_type
* @param $exp
* @param $ttl
* @return string
*/
static function createToken($login_id,$login_type,$exp=7*24*3600,$ttl=24*3600): string
{
$key = getenv("TOKEN_KEY");
$payload = array(
// "iss" => "http://example.org",//暫時用不到
// "aud" => "http://example.com",//暫時用不到
"iat" => time(),
"nbf" => time(),
"exp" => time()+$exp,//過期時間
"ttl" => time()+$exp+$ttl,//刷新時間
"login_id" => $login_id,
"login_type" => $login_type,
);
return JWT::encode($payload,$key,'HS256');
}
/**
* 解析token
* @param $token
* @return array
*/
static function analyseToken($token): array
{
$key = getenv("TOKEN_KEY");
$decode = JWT::decode($token, new Key($key, 'HS256'));
//失效
if(time() > $decode->ttl){
return noticeMsg(300,"令牌失效");
}
//過期
if(time() > $decode->exp){
$token = JwtService::createToken($decode->login_id,$decode->login_type);
return noticeMsg(201,"令牌已刷新 請保存新令牌",[
'login_id' => $decode->login_id,
'login_type' => $decode->login_type,
'token' => $token
]);
}
return noticeMsg(200,"success",[
'login_id' => $decode->login_id,
'login_type' => $decode->login_type,
]);
}
}
~~~
~~~
標準聲明:JWT標準規定的聲明,但不是必須填寫的;
標準聲明字段:
接收該JWT的一方
iss: jwt簽發者
sub: jwt所面向的用戶
aud: 接收jwt的一方
exp: jwt的過期時間,過期時間必須要大于簽發時間
nbf: 定義在什么時間之前,某個時間點后才能訪問
iat: jwt的簽發時間
ttl : 刷新時間
jti: jwt的唯一身份標識,主要用來作為一次性token。
其他聲明:自己定義的字段,因為這部分是可以解開的,建議不要加入敏感信息,這里的data就是我自己定義的聲明
~~~
~~~
public function jwt(Request $request)
{
$key = "wml123456789";
$payload = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => time(),
"nbf" => time(),
"exp" => time()+2,//過期時間
"ttl" => time()+4//刷新時間
);
/**
* IMPORTANT:
* You must specify supported algorithms for your application. See
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
* for a list of spec-compliant algorithms.
*/
$jwt = JWT::encode($payload, $key, 'HS256');//生成 token
try {
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));//驗證token
echo '<pre>';
var_dump($decoded);
} catch(\Firebase\JWT\SignatureInvalidException $e) { //簽名不正確
echo $e->getMessage();
}catch(\Firebase\JWT\BeforeValidException $e) { // 簽名在某個時間點之后才能用
echo $e->getMessage();
}catch(\Firebase\JWT\ExpiredException $e) { // token過期
echo $e->getMessage();
}catch(\Exception $e) { //其他錯誤
echo $e->getMessage();
}
return 'end';
}
~~~
### 手寫的第二種
生成 token
~~~
$token = (new jwt())->aud('app')->sub($info->id)->ttl(7*24*60)->get_token();
~~~
校驗 token
~~~
$jwt = new jwt();
$res = $jwt->parse_toekn($request);//檢測token是否為空
if(is_array($res)) return response(errorMsg($res[1],$res[0]));
$res = $jwt->check_sign();//檢測token是否正確
if(is_array($res)) return response(errorMsg($res[1],$res[0]));
$res = $jwt->check();//檢測token是否過期
if(is_array($res)) return response(errorMsg($res[1],$res[0]));
if($jwt->body_arr->aud != $source) return response(errorMsg(493,"token 驗證失敗!!"));
// 將id掛載到request中
return ['login_id' => $jwt->body_arr->sub];
~~~
jwt.php
~~~
<?php
// 自定義jwt
class jwt{
// 簽名算法
private $alg = 'sha256';
// 令牌的類型
private $typ = 'JWT';
// 簽發人
private $iss;
// 主題
private $sub = 'token';
// 受眾
private $aud;
// 過期時間
private $exp;
// 生效時間,在此之前是無效的
private $nbf;
// 簽發時間
private $iat;
// 編號
private $jti;
// 過期時間
private $ttl = 120;
// 過期刷新時間
private $refresh_ttl = 240;
// 頭部數據
private $head;
// 頭部數組
public $head_arr;
// 載體
private $body;
// 載體數組
public $body_arr;
// 簽名
private $sign;
// token
public $token;
// jwt秘鑰
private $jwt_secret;
// 生成頭部
public function __construct(){
$this->jwt_secret = getenv('JWT_SECRET');
$this->ttl = getenv('JWT_TTL') ? getenv('JWT_TTL') : $this->ttl;
$this->refresh_ttl = getenv('JWT_REFRESH_TTL') ? getenv('JWT_REFRESH_TTL') : $this->refresh_ttl;
}
// 設置過期時間
public function ttl($m){
$this->ttl = $m;
return $this;
}
// 設置刷新時間
public function refresh_ttl($m){
$this->refresh_ttl = $m;
return $this;
}
// 設置主題
public function sub($m){
$this->sub = $m;
return $this;
}
// 設置受眾
public function aud($m){
$this->aud = $m;
return $this;
}
// 生成jwt頭
protected function set_head(){
$this->head_arr = [
'alg' => $this->alg,
'typ' => $this->typ,
];
$this->head = $this->base64url_encode(json_encode($this->head_arr));
}
// 生成jwt載荷
protected function set_body(){
$this->iat = time();
$this->body_arr = [
'sub' => $this->sub,
'aud' => $this->aud,
'iat' => $this->iat,
'exp' => $this->iat + $this->ttl * 60,
];
$this->body = $this->base64url_encode(json_encode($this->body_arr));
}
// 設置簽名
protected function set_sign(){
$this->set_head();
$this->set_body();
$this->sign = $this->base64url_encode(hash_hmac('sha256', $this->head . '.' . $this->body, $this->jwt_secret));
}
public function check_sign(){
$sign = $this->base64url_encode(hash_hmac('sha256', $this->head . '.' . $this->body, $this->jwt_secret));
if($sign != $this->sign) return [493, 'token簽名錯誤'];
}
// 設置token
protected function set_token(){
$this->set_sign();
$this->token = $this->head . '.' . $this->body . '.' . $this->sign;
}
// 重置token
protected function reset_token(){
$this->set_token();
return $this->token;
}
// 生成jwt
public function get_token(){
if($this->token){
return $this->token;
}
return $this->reset_token();
}
// 添加需要解析的token
public function add_token($token){
$this->token = $token;
return $this;
}
// 解析token
public function parse_toekn($request){
$token = $request->token ?? '';
$token = $token ? $token : $this->token;
if(!$token){
// 從header中獲取
$app_token = $request->header('Authorization', '');
if(empty($app_token)){
return [493,"token格式不正確"];
}
$token = explode(' ', $app_token);
if(!isset($token[1])) return [493,"token不存在"];
$token = $token[1];
}
$this->token = $token;
$arr = explode('.', $token);
$this->head = $arr[0];
$this->head_arr = json_decode(base64_decode($arr[0]));
$this->body = $arr[1];
$this->body_arr = json_decode(base64_decode($arr[1]));
$this->exp = $this->body_arr->exp;
$this->sign = $arr[2];
// $this->check_sign();
return $this;
}
// 驗證token有效期
public function check(){
// 驗證token有效期
if($this->exp < time()){
return [493, 'token過期'];
}
return $this;
}
// 刷新token
public function refresh_token(){
$this->can_refresh();
$this->sub = $this->body_arr->sub;
$this->aud = $this->body_arr->aud;
return $this->reset_token();
}
// 是否能刷新token
private function can_refresh(){
if(!$this->token){
return [493, 'token不存在'];
}
// 驗證token能否刷新
if($this->exp < (time() - $this->refresh_ttl * 60)){
return [493, 'token刷新期已過'];
}
return $this;
}
// 生成base64
protected function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
}
~~~
- 文檔說明
- 開始
- linux
- 常用命令
- ps -ef
- lsof
- netstat
- 解壓縮
- 復制
- 權限
- 其他
- lnmp集成安裝
- supervisor
- 安裝
- supervisor進程管理
- nginx
- 域名映射
- 負載均衡配置
- lnmp集成環境安裝
- nginx源碼安裝
- location匹配
- 限流配置
- 日志配置
- 重定向配置
- 壓縮策略
- nginx 正/反向代理
- HTTPS配置
- mysql
- navicat創建索引
- 設置外網鏈接mysql
- navicat破解
- sql語句學習
- 新建mysql用戶并賦予權限
- php
- opcache
- 設計模式
- 在CentOS下安裝crontab服務
- composer
- 基礎
- 常用的包
- guzzle
- 二維碼
- 公共方法
- 敏感詞過濾
- IP訪問頻次限制
- CURL
- 支付
- 常用遞歸
- 數據排序
- 圖片相關操作
- 權重分配
- 毫秒時間戳
- base64<=>圖片
- 身份證號分析
- 手機號相關操作
- 項目搭建 公共處理函數
- JWT
- 系統函數
- json_encode / json_decode 相關
- 數字計算
- 數組排序
- php8
- jit特性
- php8源碼編譯安裝
- laravel框架
- 常用artisan命令
- 常用查詢
- 模型關聯
- 創建公共方法
- 圖片上傳
- 中間件
- 路由配置
- jwt
- 隊列
- 定時任務
- 日志模塊
- laravel+swoole基本使用
- 拓展庫
- 請求接口log
- laravel_octane
- 微信開發
- token配置驗證
- easywechart 獲取用戶信息
- 三方包
- webman
- win下熱更新代碼
- 使用laravel db listen 監聽sql語句
- guzzle
- 使用workman的httpCLient
- 修改隊列后代碼不生效
- workman
- 安裝與使用
- websocket
- eleticsearch
- php-es 安裝配置
- hyperf
- 熱更新
- 安裝報錯
- swoole
- 安裝
- win安裝swoole-cli
- google登錄
- golang
- 文檔地址
- 標準庫
- time
- 數據類型
- 基本數據類型
- 復合數據類型
- 協程&管道
- 協程基本使用
- 讀寫鎖 RWMutex
- 互斥鎖Mutex
- 管道的基本使用
- 管道select多路復用
- 協程加管道
- beego
- gin
- 安裝
- 熱更新
- 路由
- 中間件
- 控制器
- 模型
- 配置文件/conf
- gorm
- 初始化
- 控制器 模型查詢封裝
- 添加
- 修改
- 刪除
- 聯表查詢
- 環境搭建
- Windows
- linux
- 全局異常捕捉
- javascript
- 常用函數
- vue
- vue-cli
- 生產環境 開發環境配置
- 組件通信
- 組件之間通信
- 父傳子
- 子傳父
- provide->inject (非父子)
- 引用元素和組件
- vue-原始寫法
- template基本用法
- vue3+ts項目搭建
- vue3引入element-plus
- axios 封裝網絡請求
- computed 計算屬性
- watch 監聽
- 使用@符 代替文件引入路徑
- vue開發中常用的插件
- vue 富文本編輯
- nuxt
- 學習筆記
- 新建項目踩坑整理
- css
- flex布局
- flex PC端基本布局
- flex 移動端基本布局
- 常用css屬性
- 盒子模型與定位
- 小說分屏顯示
- git
- 基本命令
- fetch
- 常用命令
- 每次都需要驗證
- git pull 有沖突時
- .gitignore 修改后不生效
- 原理解析
- tcp與udp詳解
- TCP三次握手四次揮手
- 緩存雪崩 穿透 更新詳解
- 內存泄漏-內存溢出
- php_fpm fast_cgi cig
- redis
- 相關三方文章
- API對外接口文檔示范
- elaticsearch
- 全文檢索
- 簡介
- 安裝
- kibana
- 核心概念 索引 映射 文檔
- 高級查詢 Query DSL
- 索引原理
- 分詞器
- 過濾查詢
- 聚合查詢
- 整合應用
- 集群
- docker
- docker 簡介
- docker 安裝
- docker 常用命令
- image 鏡像命令
- Contrainer 容器命令
- docker-compose
- redis 相關
- 客戶端安裝
- Linux 環境下安裝
- uni
- http請求封裝
- ios打包
- 視頻縱向播放
- 日記
- 工作日記
- 情感日志
- 壓測
- ab
- ui
- thorui
- 開發規范
- 前端
- 后端
- 狀態碼
- 開發小組未來規劃