PHPackages                             mzh/hyperf-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. mzh/hyperf-jwt

ActiveLibrary

mzh/hyperf-jwt
==============

hyperf-jwt 优化版

v0.1.5(5y ago)71.9k1MITPHPPHP &gt;=7.2

Since Dec 23Pushed 5y ago2 watchersCompare

[ Source](https://github.com/lphkxd/hyperf-jwt)[ Packagist](https://packagist.org/packages/mzh/hyperf-jwt)[ RSS](/packages/mzh-hyperf-jwt/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (7)Versions (7)Used By (0)

### 基于Hyperf() 框架的 jwt 鉴权

[](#基于hyperfhttpsdochyperfiozhreadme-框架的-jwt-鉴权)

 [![Stable Version](https://camo.githubusercontent.com/e7ec0574abcd425c97976ac2127734fd797fbe6b917035591cbabdd7bccacf79/68747470733a2f2f706f7365722e707567782e6f72672f6d7a682f6879706572662d6a77742f762f737461626c65)](https://github.com/lphkxd/hyperf-jwt/releases) [![Build Status](https://camo.githubusercontent.com/9b42d4888900b47a3e567698fab18634b5e0d3f40788b556beb6fd9abf3fa2be/68747470733a2f2f7472617669732d63692e6f72672f6d7a682f6879706572662d6a77742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mzh/hyperf-jwt) [![Total Downloads](https://camo.githubusercontent.com/2c061a03594a6f97140ccbafec0c5237f06002fd4e2a5b7080a78fc44d746369/68747470733a2f2f706f7365722e707567782e6f72672f6d7a682f6879706572662d6a77742f646f776e6c6f616473)](https://packagist.org/packages/mzh/hyperf-jwt) [![Monthly Downloads](https://camo.githubusercontent.com/1ec3ef8136e9fd171451956da5a205add09ab6319e4cb7c25573fa0f3f8f651d/68747470733a2f2f706f7365722e707567782e6f72672f6d7a682f6879706572662d6a77742f642f6d6f6e74686c79)](https://packagist.org/packages/mzh/hyperf-jwt) [![Php Version](https://camo.githubusercontent.com/c197ef1e57a32c886b2a54d02ead7dbc035f18b3f39a642703b9372eef7819f9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://www.php.net) [![Swoole Version](https://camo.githubusercontent.com/0a5cfeaacb96598473ccdbddfedeaf286e434fa5c166ed3b382c92575af33dfa/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e352d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://github.com/swoole/swoole-src) [![ License](https://camo.githubusercontent.com/e1df71423fa8e14a0f0f946c54056746a1fdf3139c786ab12227ccc208730464/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c70686b78642f6879706572662d6a77742e7376673f6d61784167653d32353932303030)](https://github.com/lphkxd/hyperf-jwt/blob/master/LICENSE)

### 思路来源与 ([https://github.com/phper666/jwt-auth)组件](https://github.com/phper666/jwt-auth)%E7%BB%84%E4%BB%B6)

[](#思路来源与-httpsgithubcomphper666jwt-auth组件)

### 重构sso单点登录token失效逻辑

[](#重构sso单点登录token失效逻辑)

### 说明：

[](#说明)

> `hyperf-jwt` 支持单点登录、多点登录、支持注销 token(token会失效)、支持refresh换取新token 失效老token

> 单点登录：只会有一个 token 生效，一旦刷新 token ，前面生成的 token 都会失效，一般以用户 id 来做区分

> 多点登录：token 不做限制

> 单点登录原理：token版本号，`JWT` 单点登录必须用到 aud（接收方） 默认字段，`aud` 字段的值默认为用户 id。当生成 token 时，会更新白名单uid的key值为当前的版本号，但是如果是调用 `refreshToken` 来刷新 token 或者调用 `logout` 注销token，默认前面生成的 token 都会失效。
> 如果开启单点登录模式，每次验证时候会查询当前uid的对应key是否和当前的版本号对应

> 多点登录原理：暂未实现

> token 不做限制原理：token 不做限制，在 token 有效的时间内都能使用

### 使用：

[](#使用)

##### 1、安装依赖

[](#1安装依赖)

```
composer require mzh/hyperf-jwt
```

##### 2、发布配置

[](#2发布配置)

```
php bin/hyperf.php jwt:publish --config
```

##### 3、jwt配置

[](#3jwt配置)

去配置 `config/autoload/jwt.php` 文件或者在配置文件 `.env` 里配置

```
# 务必改为你自己的字符串
JWT_SECRET=hyperf
#token过期时间，单位为秒
JWT_TTL=60
```

更多的配置请到 `config/autoload/jwt.php` 查看

##### 4、模拟登录获取token

[](#4模拟登录获取token)

```
setIssuer('api');
            #... 设置更多token属性

            #... 设置data数据
            $jwtData->setJwtData(['uid' => 123,'type' => 1111,'group' => 1]);

            #返回 JwtBuilder对象
            $tokenObj = $jwt->createToken($jwtData);

            #获取生成的token
            $tokenObj->getToken();

            #用法2 传入数组
            #返回 JwtBuilder对象
            $tokenObj = $jwt->createToken(['uid' => $id,'type' => $type,'group' => $group]);

            #获取生成的token
            $tokenObj->getToken();

          #获取刷新token 传入数组  第一个参数为数据，第二个参数为类型，默认是access 可以定义为 refersh 或者其他类型自定义
            #返回 JwtBuilder对象
            $tokenObj = $jwt->createToken(['uid' => $id,'type' => $type,'group' => $group],Jwt::SCOPE_REFRESH);

            #获取生成的token
            $tokenObj->getToken();

        return $tokenObj->getToken();
    }
}
```

注意：支持传入用户对象获取 token，支持token类型，

##### 5、使用例子参考

[](#5使用例子参考--httpsgithubcomlphkxdhyperf-admin)

##### 6、建议

[](#6建议)

> 目前 `jwt` 抛出的异常目前有两种类型 `Mzh\JwtAuth\Exception\TokenValidException` 和 `Mzh\JwtAuth\Exception\JWTException,TokenValidException` 异常为 token 验证失败的异常，会抛出 `401` ,`JWTException` 异常会抛出 `500`，最好你们自己在项目异常重新返回错误信息

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% 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 ~37 days

Recently: every ~46 days

Total

6

Last Release

2151d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6064d6ea21c78c19ac11c7918d833822adf51797f1920b2c1b45968795a71074?d=identicon)[lphkxd](/maintainers/lphkxd)

---

Top Contributors

[![lphkxd](https://avatars.githubusercontent.com/u/16346895?v=4)](https://github.com/lphkxd "lphkxd (3 commits)")[![zxyfaxcn](https://avatars.githubusercontent.com/u/2535215?v=4)](https://github.com/zxyfaxcn "zxyfaxcn (1 commits)")

---

Tags

phphyperf

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[hyperf/swow-skeleton

A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares.

514.5k](/packages/hyperf-swow-skeleton)[hyperf/odin

425.9k](/packages/hyperf-odin)[mzh/hyperf-validate

hyperf-validate 支持request 和方法场景验证，兼容tp5写法 支持控制器注解验证、方法注解验证

202.9k1](/packages/mzh-hyperf-validate)

PHPackages © 2026

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