PHPackages                             jzh/jwt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. jzh/jwt

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

jzh/jwt
=======

v0.0.7(3y ago)016MITPHP

Since May 19Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Jzh-thub/webman-jwt)[ Packagist](https://packagist.org/packages/jzh/jwt)[ RSS](/packages/jzh-jwt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

webman JWT 插件
=============

[](#webman-jwt-插件)

安装
--

[](#安装)

```
composer require jzh/jwt
```

配置文件
----

[](#配置文件)

```
// guard 角色配置
// jwt jwt参数配置

// 配置示例（根据自己真实情况 user一定要存在 因为默认就是user）
    'guard'  => [
        'user' => [
            'key'   => 'uid',    //对应表主键
            'limit' => 1,       //-1为不限制终端数量 0为只支持一个终端在线 大于0为同一账号同终端支持数量 建议设置为1 则同一账号同终端在线1个
            'model' => function ($uid) {
                // ThinkORM
                return \think\facade\Db::table('core_user')
                    ->field('uid,account,create_time')
                    ->where('uid', $uid)
                    ->find();

                // LaravelORM
                // return \support\Db::table('resty_user')
                //    ->where('id', $uid)
                //    ->select('id','email','mobile','create_time')
                //    ->first();
            }
        ],
        'admin'=>[
            'key'   => 'id',    //对应表主键
            'limit' => -1,       //-1为不限制终端数量 0为只支持一个终端在线 大于0为同一账号同终端支持数量 建议设置为1 则同一账号同终端在线1个
            'model' => function ($id) {
                // ThinkORM
                return \think\facade\Db::table('admin')
                    ->field('id,account,create_time')
                    ->where('id', $id)
                    ->find();

                // LaravelORM
                // return \support\Db::table('resty_user')
                //    ->where('id', $uid)
                //    ->select('id','email','mobile','create_time')
                //    ->first();
            }
        ]
    ],
    'jwt'    => [
        // 算法类型 ES256、HS256、HS384、HS512、RS256、RS384、RS512
        'algorithms'          => 'HS256',
        // access令牌秘钥
        'access_secret_key'   => '',
        // access令牌过期时间，单位秒。默认 2 小时
        'access_exp'          => 7200,
        // refresh令牌秘钥
        'refresh_secret_key'  => '',
        // refresh令牌过期时间，单位秒。默认 7 天
        'refresh_exp'         => 604800,
        // refresh 令牌是否禁用,默认不禁用
        'refresh_disable'     => false,
        // 缓存令牌前缀
        'redis_pre'           => 'JZH:JWT:TOKEN:',
        // 令牌签发者
        'iss'                 => 'webman',
        /**
         * access令牌 RS256 私钥
         * 生成RSA私钥(Linux系统)：openssl genrsa -out access_private_key.key 1024 (2048)
         */
        'access_private_key'  => '',
        /**
         * access令牌 RS256 公钥
         * 生成RSA公钥(Linux系统)：openssl rsa -in access_private_key.key -pubout -out access_public_key.key
         */
        'access_public_key'   => '',

        /**
         * refresh令牌 RS256 私钥
         * 生成RSA私钥(Linux系统)：openssl genrsa -out refresh_private_key.key 1024 (2048)
         */
        'refresh_private_key' => '',
        /**
         * refresh令牌 RS256 公钥
         * 生成RSA公钥(Linux系统)：openssl rsa -in refresh_private_key.key -pubout -out refresh_public_key.key
         */
        'refresh_public_key'  => '',
    ]
```

异常类
---

[](#异常类)

> 失败，抛出`JwtException`异常

使用
--

[](#使用)

指定角色
----

[](#指定角色)

```
$guard = 'admin'; // 默认user
\Jzh\Jwt\Facade\JWT::guard('admin');
```

生成令牌
----

[](#生成令牌)

```
$user = [
    'id'  => id, // 这里必须是一个全局抽象唯一id
    'xx'  => 'xx',
    'xx1' => 'xx1'
];
$token = \Jzh\Jwt\Facade\JWT::generateToken($user);
var_dump(json_encode($token));

// guard 指定 角色;比如 user 用户,admin 用户
$token = \Jzh\Jwt\Facade\JWT::guard('admin')->generateToken($user);
```

**输出（json格式）**

```
{
    "token_type": "Bearer",
    "access_token_expires": 36000,
    "access_token": "eyJ0eXAiOiJAUR-Gqtnk9LUPO8IDrLK7tjCwQZ7CI...",
    "refresh_token_expires":78000,
    "refresh_token": "eyJ0eXAiOiJIEGkKprvcccccQvsTJaOyNy8yweZc...",
}
```

**响应参数**

参数类型描述示例值token\_typestringToken 类型Beareraccess\_token\_expiresintToken凭证有效时间，单位：秒36000access\_tokenstring访问凭证webmanrefresh\_expires\_inint刷新Token凭证有效时间，单位：秒720000refresh\_token\_expiresstring刷新访问凭证webman### 退出登录

[](#退出登录)

```
$all = false; //false 退出当前用户;true 退出所有当前用户终端;默认 false
\Jzh\Jwt\Facade\JWT::logout($all);
\Jzh\Jwt\Facade\JWT::guard('admin')->logout(); //管理员退出
```

### 支持函数列表

[](#支持函数列表)

> 1、获取当前`uid`

```
// 获取user用户
$uid = \Jzh\Jwt\Facade\JWT::getCurrentId();

// 获取admin用户
$uid = \Jzh\Jwt\Facade\JWT::guard('admin')->getCurrentId();
```

> 2、获取获取会员信息

```
$cache = false; //false 获取缓存中的数据  true 获取数据库中的数据
$userInfo = \Jzh\Jwt\Facade\JWT::user($cache);

// 获取admin用户
$userInfo = \Jzh\Jwt\Facade\JWT::guard('admin')->user($cache);
```

> 3、获取指定令牌扩展内容

```
$info = \Jzh\Jwt\Facade\JWT::getExtend();

// 获取admin用户
$info = \Jzh\Jwt\Facade\JWT::guard('admin')->getExtend();
```

> 4、获取自定义字段

```
$uid = \Jzh\Jwt\Facade\JWT::getExtendVal('uid');

// 获取admin用户
$uid = \Jzh\Jwt\Facade\JWT::guard('admin')->getExtendVal('uid');
```

> 4、刷新令牌（通过刷新令牌获取访问令牌）

```
$info = \Jzh\Jwt\Facade\JWT::refreshToken();
{
    "access_token_expires": 36000,
    "access_token": "eyJ0eXAiOiJAUR-Gqtnk9LUPO8IDrLK7tjCwQZ7CI...",
}

// 获取admin用户
$info = \Jzh\Jwt\Facade\JWT::guard('admin')->refreshToken();
```

> 5、获令牌有效期剩余时长

```
$exp = \Jzh\Jwt\Facade\JWT::getTokenExp();

// 获取admin用户
$exp = \Jzh\Jwt\Facade\JWT::guard('admin')->getTokenExp();
```

> 6、验证令牌

```
// 返回 jwt 解密的信息
$info = \Jzh\Jwt\Facade\JWT::verify();

// 获取admin用户
$info = \Jzh\Jwt\Facade\JWT::guard('admin')->verify();
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

7

Last Release

1433d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/18be0a4a9d7b5cab20e5f886d8c9a3e1362b5188f6e4c625db8895e7091bc0e5?d=identicon)[Jzh-thub](/maintainers/Jzh-thub)

---

Top Contributors

[![Jzh-thub](https://avatars.githubusercontent.com/u/74651570?v=4)](https://github.com/Jzh-thub "Jzh-thub (6 commits)")

### Embed Badge

![Health badge](/badges/jzh-jwt/health.svg)

```
[![Health](https://phpackages.com/badges/jzh-jwt/health.svg)](https://phpackages.com/packages/jzh-jwt)
```

###  Alternatives

[thenetworg/oauth2-azure

Azure Active Directory OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2509.6M48](/packages/thenetworg-oauth2-azure)[stevenmaguire/oauth2-keycloak

Keycloak OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2275.9M27](/packages/stevenmaguire-oauth2-keycloak)[patrickbussmann/oauth2-apple

Sign in with Apple OAuth 2.0 Client Provider for The PHP League OAuth2-Client

1132.5M6](/packages/patrickbussmann-oauth2-apple)[socialiteproviders/microsoft

Microsoft OAuth2 Provider for Laravel Socialite

326.1M13](/packages/socialiteproviders-microsoft)[microsoft/kiota-authentication-phpleague

Authentication provider for Kiota using the PHP League OAuth 2.0 client to authenticate against the Microsoft Identity platform

153.2M7](/packages/microsoft-kiota-authentication-phpleague)[rainlab/user-plugin

User plugin for October CMS

11954.3k13](/packages/rainlab-user-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
