PHPackages                             zxyfaxcn/jwt-auth - 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. zxyfaxcn/jwt-auth

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

zxyfaxcn/jwt-auth
=================

hyperf-jwt

v3.0.1(3y ago)080↓100%MITPHPPHP &gt;=8.0

Since Aug 2Pushed 3y ago1 watchersCompare

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

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

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

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

### 思路来源与 ([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 zxyfaxcn/jwt-auth
```

##### 2、发布配置

[](#2发布配置)

```
php bin/hyperf.php vendor:publish zxyfaxcn/jwt-auth
```

##### 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');
        $jwtData->setAudience('xxx');
        #... 设置更多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建议)

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

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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 ~311 days

Total

3

Last Release

1121d ago

Major Versions

v2.0.0 → v3.0.02023-01-18

PHP version history (2 changes)v2.0.0PHP &gt;=7.2

v3.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4cbf644c1c87edf82dc04fca6167682dae6bf5ce75ef83bd833d17c3eff5eaac?d=identicon)[zxyfaxcn](/maintainers/zxyfaxcn)

---

Top Contributors

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

---

Tags

phphyperf

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[hyperf-ext/jwt

The Hyperf JWT package.

53134.9k2](/packages/hyperf-ext-jwt)[hyperf-ext/auth

The Hyperf Auth package.

2376.2k3](/packages/hyperf-ext-auth)

PHPackages © 2026

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