luzpcn / tp6-jwt-token
firebase/php-jwt integration for thinkphp6
1.0
2022-05-17 11:56 UTC
Requires
- php: ^7.4 || ^8.0
- firebase/php-jwt: v6.2.*
README
基于firebase/php-jwt封装的一个jwt工具包,在前后端分离时 它非常有用。
配置
安装完毕后会自动生成配置文件config/jwt.php
<?php
return [
//签名者
'signer' => 'HS256',
// 对称密钥
'private_key' => 'a4693602cbb7aee',
//token获取name
'header_name' => 'Authorization',
'header_prefix' => 'Bearer',
'param_name' => 'token',
//有效期(默认两个小时) 单位:秒
'exp' => 3600 * 2
];
使用
使用方式非常简单,传递token参数的,你可以选择Header、Param
提供3个方法create(array $payload)
,auth()
,invalidate(string $token_id, string $msg)
。
生成token
示例:通过用户id生成token
$token = Token::create(['id'=>100]);
验证token
用于验证token,返回值是一个$payload数组
try {
$payload = Token::auth();
} catch (\Exception $e) {
$e->getMessage()//失败
}
无效token
通过把token存储到缓存中,用的是tp6框架缓存cache
方法
Token::invalidate(string $token_id, string $msg);
再使用上面的auth
方法就会--失败异常--token无效:$msg