yaobiao/think-jwt

ThinkPHP Jwt Component

Installs: 106

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 18

Type:think-extend

v1.0.11 2021-10-29 09:38 UTC

README

Build Status Scrutinizer Code Quality Code Coverage

只支持 thinkphp 6.0

安装

稳定版

$ composer require xiaodi/think-jwt

开发版

$ composer require xiaodi/think-jwt:dev-master

使用

  1. 命令生成签名key
$ php think jwt:make
  1. 配置 config/jwt.php

完整多应用配置

<?php

return [
    'default' => 'admin',
    'apps' => [
        'admin' => [
            'token' => [
                'uniqidKey'    => 'uid',
                'signerKey'    => '',
                'notBefore'    => 0,
                'expiresAt'    => 3600,
                'refreshTTL'   => 7200,
                'signer'       => 'Lcobucci\JWT\Signer\Hmac\Sha256',
                'type'         => 'Header',
                'refresh'      => 50001,
                'relogin'      => 50002,
                'iss'          => '',
                'aud'          => '',
                'automaticRenewal' => false,
            ],
            'user' => [
                'bind' => false,
                'model'  => '',
            ]
        ]
    ],
    'manager' => [
        // 缓存前缀
        'prefix' => 'jwt',
        // 黑名单缓存名
        'blacklist' => 'blacklist',
        // 白名单缓存名
        'whitelist' => 'whitelist'
    ]
];

token

  • uniqidKey 用户唯一标识
  • signerKey 密钥
  • notBefore 时间前不能使用 默认生成后直接使用
  • expiresAt Token有效期(秒)
  • signer 加密算法
  • type 获取 Token 途径
  • refresh Token过期抛异常code = 50001
  • relogin Token失效异常code = 50002
  • automaticRenewal 开启过期自动续签

user

  • bind 是否注入用户模型(中间件有效)
  • model 用户模型文件

blacklist

  • cacheKey 黑名单缓存key

以下两个异常都会抛一个HTTP异常 StatusCode = 401

  • xiaodi\Exception\HasLoggedException
  • xiaodi\Exception\TokenAlreadyEexpired

Token 生成

use xiaodi\JWTAuth\Facade\Jwt;

public function login()
{
    //...登录判断逻辑

    // 默认应用
    return json([
        'token' => Jwt::token(['uid' => 1]),
        'token_type' => Jwt::type(),
        'expires_in' => Jwt::ttl(),
        'refresh_in' => Jwt::refreshTTL()
    ]);
    
    // 指定应用
    return json([
        'token' => Jwt::store('wechat')->token(['uid' => 1]),
        'token_type' => Jwt::type(),
        'expires_in' => Jwt::ttl(),
        'refresh_in' => Jwt::refreshTTL()
    ]);
}

Token 验证

手动验证

use xiaodi\JWTAuth\Facade\Jwt;
use xiaodi\JWTAuth\Exception\HasLoggedException;
use xiaodi\JWTAuth\Exception\TokenAlreadyEexpired;

class User {

    public function test()
    {
        try {
            // 默认应用
            Jwt::verify($token);
            
            // 指定应用
            // Jwt::store('wechat')->verify($token);
        } catch (HasLoggedException $e) {
            // 已在其它终端登录
        } catch (TokenAlreadyEexpired $e) {
            // Token已过期
        }
        
        // 验证成功
        // 如 开启用户注入功能 可获取当前用户信息
        dump(Jwt::user());
    }
}

路由验证

use xiaodi\JWTAuth\Middleware\Jwt;

// 默认应用
Route::get('/hello', 'index/index')->middleware(Jwt::class);

// 指定应用
Route::get('/hello', 'index/index')->middleware(Jwt::class, 'wechat');

Token 自动获取

支持以下方式自动获取

  • Header
  • Cookie
  • Url

赋值方式

类型 途径 标识
Header Authorization Bearer Token
Cookie Cookie token
Url Request token
# config/jwt.php

<?php

return [

    'apps' => [
        'admin' => [
            'token' => [
                // ...其它配置
                'type' => 'Header',
                // 'type' => 'Cookie',
                // 'type' => 'Url',
                // 支持多种方式获取
                // 'type' => 'Header|Url',
            ]
        ]
    ]
    
];

过期自动续签

app/config/jwt.php

automaticRenewal => true

系统检测到 Token 已过期, 会自动续期并返回以下 header 信息。

  • Automatic-Renewal-Token
  • Automatic-Renewal-Token-RefreshAt

前端需要接收最新 Token,下次异步请求时,携带此 Token。

注销应用Token

注销指定应用下缓存的用户 (强制下线 重新登录)

$store = 'wechat';

app('jwt.manager')->resetStoreWhiteToken($store);