zl程序教程

您现在的位置是:首页 >  其他

当前栏目

jwt token 鉴权验证 【firebase 5.x】

2023-02-19 12:19:28 时间

1. JWT介绍

本文是在 TP6.0 使用 JWT 的示例

JWT全称: JSON Web Token,以 token 的方式代替传统的 cookie、session 模式,用于各服务器、客户端传递信息及签名验证

2. 新增自定义函数 fault()

app/common.php 中新增以下函数,用于抛出异常

/** * 抛出异常错误 * * @param string  $msg * @param integer $code */function fault(string $msg = "", $code = 201){    throw new \Exception($msg, $code);}

3. 新增配置文件 jwt.php

在全局配置目录 config 目录下新建 jwt.php 文件,文件内容如下

<?php// +----------------------------------------------------------------------// | JWT (Json Web Token) 配置// +----------------------------------------------------------------------// | Author: liang 23426945@qq.com// +----------------------------------------------------------------------return [    'iss'             => 'liang',   // 签发者    'aud'            => 'chen',    // 接收者    'key'            => 'yang',    // 访问密钥    'prefix'         => 'jwt_',    // 缓存前缀    'exp'            => 864000,    // 过期时间,864000秒=10天    'single_sign_on' => false,      // 单点登录 true 开启 false 关闭];

4. JWT 功能封装类

安装扩展包

composer require firebase/php-jwt:'5.*'
<?php// +----------------------------------------------------------------------// | JWT (Json Web Token) 功能封装类// +----------------------------------------------------------------------// | Author: liang 23426945@qq.com// +----------------------------------------------------------------------declare(strict_types=1);use Firebase\JWT\JWT;class JwtAuth{    // +------------------------------------------------------------------    // | 初始化配置    // +------------------------------------------------------------------    /**     * 初始化配置     */    public function __construct()    {        $this->iss    = config('jwt.iss');     //签发者 可选        $this->aud    = config('jwt.aud');     //接收该JWT的一方,可选        $this->exp    = config('jwt.exp');     //过期时间,864000秒 = 10天        $this->key    = config('jwt.key');     //访问秘钥        $this->prefix = config('jwt.prefix');  //缓存前缀    }    // +------------------------------------------------------------------    // | 创建、解析 token    // +------------------------------------------------------------------    /**     * 创建token     *     * @param  array  $data     * @return string token     */    public function encode(array $data)    {        $time = time(); //当前时间        $token = [            'iss'  => $this->iss,           //签发者 可选            'aud'  => $this->aud,           //接收该JWT的一方,可选            'iat'  => $time,                //签发时间            'nbf'  => $time,                //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用            'exp'  => $time + $this->exp,   //过期时间            'data' => $data,                //附加数据        ];        $token = JWT::encode($token, $this->key); // 创建token        $this->cache($data['uid'], $token); // 将token存入缓存        return $token; // 返回token    }    /**     * 解析token     *     * @param string $token 前端请求携带的token     */    public function decode(string $token)    {        try {            JWT::$leeway = 0; //当前时间减去60,把时间留点余地            return JWT::decode($token, $this->key, ['HS256']); //HS256方式,这里要和签发的时候对应        } catch (\Firebase\JWT\SignatureInvalidException $e) {  //签名不正确            fault('签名不正确');        } catch (\Firebase\JWT\BeforeValidException $e) {  // 签名在某个时间点之后才能用            fault('登录未生效');        } catch (\Firebase\JWT\ExpiredException $e) {  // token过期            fault('登录过期');        } catch (\Exception $e) {  //其他错误            fault($e->getMessage());        }    }    // +------------------------------------------------------------------    // | 检测token    // +------------------------------------------------------------------    /**     * 将用户token存入缓存,用于单点登录校验     *     * @param int    $id    用户id     * @param string $token 服务器端生成的token     */    private function cache(int $uid, string $token)    {        // 缓存token        cache($this->prefix . $uid, $token);    }    /**     * 检测token是否已过期(单点登录)     *     * @param  int     $id    用户id     * @param  string  $token 前端请求携带的token     * @return boolean true   token 有效 false 已过期     */    public function<span class="hlj