All Projects → friendsofthinkphp → think-jwt

friendsofthinkphp / think-jwt

Licence: Apache-2.0 license
ThinkPHP Jwt 扩展包

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to think-jwt

think-whoops
Whoops 接管 ThinkPHP6 异常服务
Stars: ✭ 37 (-47.89%)
Mutual labels:  thinkphp, thinkphp-component
think-permission
ThinkPHP 6 权限认证
Stars: ✭ 32 (-54.93%)
Mutual labels:  thinkphp, thinkphp-component
Thinkphp5 Wechat
微信 SDK for thinkphp5, 基于 overtrue/wechat
Stars: ✭ 133 (+87.32%)
Mutual labels:  thinkphp
Think Wechat
easywechat for thinkphp support
Stars: ✭ 250 (+252.11%)
Mutual labels:  thinkphp
Ruhua
如花商城系统thinkphp6+uniapp,小程序直播拼团限时分销APP商城
Stars: ✭ 181 (+154.93%)
Mutual labels:  thinkphp
Thinkphp Bjyadmin
ThinkPHP 整合 Auth 权限管理、支付宝、微信支付、阿里 OSS、友盟推送、融云即时通讯、云通讯短信、Email、Excel、PDF 等等,基于 ThinkPHP 扩展了大量的功能,而不改动 ThinkPHP 核心,非常方便的升级、移植和使用。
Stars: ✭ 1,733 (+2340.85%)
Mutual labels:  thinkphp
Search Engine Rank
🐘根据网站关键词,获取网站在各大搜索引擎(百度、360、搜狗)的排名情况,有利于网站seo
Stars: ✭ 197 (+177.46%)
Mutual labels:  thinkphp
Shopxo
ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ)、APP、多仓库、多商户,遵循MIT开源协议发布、基于ThinkPHP6框架研发
Stars: ✭ 1,795 (+2428.17%)
Mutual labels:  thinkphp
acgice
🍭 Acgice search A multi-language aggregate search engine , Acgice 搜索 基于TP5的一个多语言的聚合搜索引擎。
Stars: ✭ 23 (-67.61%)
Mutual labels:  thinkphp
Crmeb wechatminiprogram
CRMEBv2.6以客户管理为中心+电商营销系统,微信小程序商城,带分销、秒杀、积分、优惠券等功能
Stars: ✭ 158 (+122.54%)
Mutual labels:  thinkphp
Pinche xcx data
同城拼车微信小程序后端代码
Stars: ✭ 244 (+243.66%)
Mutual labels:  thinkphp
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+118.31%)
Mutual labels:  thinkphp
Think Wxminihelper
基于ThinkPHP5.0的微信小程序登录流程封装。
Stars: ✭ 151 (+112.68%)
Mutual labels:  thinkphp
Framework
ThinkPHP Framework
Stars: ✭ 2,399 (+3278.87%)
Mutual labels:  thinkphp
Think Casbin
专为ThinkPHP定制的Casbin的扩展包,Casbin是一个功能强大,高效的开源访问控制库。
Stars: ✭ 138 (+94.37%)
Mutual labels:  thinkphp
lake-admin
lake-admin是一款基于ThinkPHP6和Layui的后台开发框架。
Stars: ✭ 28 (-60.56%)
Mutual labels:  thinkphp
Think Admin
ThinkPHP 6.0 与 Ant Design Pro Vue 基础前后分离权限系统
Stars: ✭ 131 (+84.51%)
Mutual labels:  thinkphp
Identity Card
A simple proof of identity card of the people's Republic of China.
Stars: ✭ 154 (+116.9%)
Mutual labels:  thinkphp
Searchku
searchku 信息查询软件,没有数据。
Stars: ✭ 199 (+180.28%)
Mutual labels:  thinkphp
A3Mall
A3Mall B2C开源商城系统使用Thinkphp6开源框架,前端采用uniapp开发,支持微信公众号商城、H5商城、小程序商城、APP商城、PC商城,前后端源码100%开源,支持免费商用。
Stars: ✭ 142 (+100%)
Mutual labels:  thinkphp

think-jwt

Build Status Scrutinizer Code Quality Code Coverage

只支持 thinkphp 6.0

安装

稳定版

$ composer require xiaodi/think-jwt

开发版

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

使用

  1. 配置 config/jwt.php

完整配置

<?php

return [
    'stores' => [
        // 单应用时 默认使用此配置
        'default' => [
            'sso' => [
                'enable' => false,
            ],
            'token' => [
                'signer_key'    => 'tant',
                'public_key'    => 'file://path/public.key',
                'private_key'   => 'file://path/private.key',
                'not_before'    => 0,
                'expires_at'    => 3600,
                'refresh_ttL'   => 7200,
                'signer'       => 'Lcobucci\JWT\Signer\Hmac\Sha256',
                'type'         => 'Header',
                'relogin_code'      => 50001,
                'refresh_code'      => 50002,
                'iss'          => 'client.tant',
                'aud'          => 'server.tant',
                'automatic_renewal' => false,
            ],
            'user' => [
                'bind' => false,
                'class'  => null,
            ]
        ],
        // 多应用时 对应应用的配置
        'admin' => [
            'sso' => [
                'enable' => false,
            ],
            'token' => [
                'signer_key'    => 'tant',
                'not_before'    => 0,
                'expires_at'    => 3600,
                'refresh_ttL'   => 7200,
                'signer'       => 'Lcobucci\JWT\Signer\Hmac\Sha256',
                'type'         => 'Header',
                'relogin_code'      => 50001,
                'refresh_code'      => 50002,
                'iss'          => 'client.tant',
                'aud'          => 'server.tant',
                'automatic_renewal' => false,
            ],
            'user' => [
                'bind' => false,
                'class'  => null,
            ]
        ]
    ],
    'manager' => [
        // 缓存前缀
        'prefix' => 'jwt',
        // 黑名单缓存名
        'blacklist' => 'blacklist',
        // 白名单缓存名
        'whitelist' => 'whitelist'
    ]
];

token

  • signer_key 密钥
  • not_before 时间前不能使用 默认生成后直接使用
  • refresh_ttL Token有效期(秒)
  • signer 加密算法
  • type 获取 Token 途径
  • relogin_code Token过期抛异常code = 50001
  • refresh_code Token失效异常code = 50002
  • automatic_renewal 开启过期自动续签

user

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

manager

  • prefix 缓存前缀
  • blacklist 黑名单缓存名
  • whitelist 白名单缓存名

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

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

缓存支持

  • File
  • Redis

Token 生成

namespace app\home\controller\Auth;

use xiaodi\JWTAuth\Facade\Jwt;

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

    // 自动获取当前应用下的jwt配置
    return json([
        'token' => Jwt::token($uid, ['params1' => 1, 'params2' => 2])->toString(),
    ]);
    
    // 自定义用户模型
    return json([
        'token' => Jwt::token($uid, ['model' => CustomMember::class])->toString(),
    ]);
}

Token 验证

自动获取当前应用(多应用下)配置。

手动验证

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

class User {

    public function test()
    {
        if (true === Jwt::verify($token)) {
            // 验证成功
        }
        
        // 验证成功
        // 如配置用户模型文件 可获取当前用户信息
        dump(Jwt::user());
    }
}

路由验证

use xiaodi\JWTAuth\Middleware\Jwt;

// 自动获取当前应用配置
Route::get('/hello', 'index/index')->middleware(Jwt::class);

// 自定义应用 使用api应用配置
Route::get('/hello', 'index/index')->middleware(Jwt::class, 'api');

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')->destroyStoreWhitelist($store);

注销应用Token(指定某个)

注销指定某个用户(强制下线 重新登录)

$store = 'wechat';
$uid = '9527';

app('jwt.manager')->destroyToken($id, $store);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].