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

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

tengfei31/jwt-auth
==================

jwt-auth Component

v1.1(3y ago)013MITPHPPHP &gt;=7.4

Since Feb 17Pushed 3y agoCompare

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

READMEChangelogDependencies (2)Versions (3)Used By (0)

### 基于Hyperf() 框架的 jwt 鉴权(json web token)组件。

[](#基于hyperfhttpsdochyperfiozhreadme-框架的-jwt-鉴权json-web-token组件)

### 采用基于 进行封装。

[](#采用基于httpsgithubcomlcobuccijwttree33-进行封装)

### 黑名单的设置参考了这篇文章

[](#黑名单的设置参考了这篇文章httpslearnkucomarticles17883)

### 注意：

[](#注意)

1、只支持php &gt;= 7.4版本，如果php &lt; 7.4，请使用3.x版本
2、支持多应用单点登录、多应用多点登录
3、如有建议欢迎给我邮件，
4、hyperf请使用2.x版本以上

你可以：

```
composer require phper666/jwt-auth:~4.0.0
composer require hyperf/utils:~2.0.0
composer require hyperf/cache:~2.0.0
composer require hyperf/command:~2.0.0
composer require hyperf/config:~2.0.0
composer require hyperf/di:~2.0.0

```

### 说明：

[](#说明)

> `jwt-auth` 支持多应用单点登录、多应用多点登录、多应用支持注销 token(token会失效)、支持多应用刷新 token

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

> 多应用多点登录：在该配置应用下token 不做限制，一旦刷新 token ，则当前配置应用的 token 会失效

> 注意：使用多应用单点登录或者多应用多点登录时，必须要开启黑名单，并且使用 `Hyperf` 的缓存(建议使用 `redis` 缓存)。如果不开启黑名单，无法使 token 失效，生成的 token 会在有效时间内都可以使用(未更换证书或者 secret )。

> 多应用单点登录原理：`JWT` 有七个默认字段供选择。单点登录主要用到 jti 默认字段，`jti` 字段的值默认为缓存到redis中的key(该key的生成为场景值+存储的用户id(`sso_key`))，这个key的值会存一个签发时间，token检测会根据这个时间来跟token原有的签发时间对比，如果token原有时间小于等于redis存的时间，则认为无效

> 多应用多点登录原理：多点登录跟单点登录差不多，唯一不同的是jti的值不是场景值+用户id(`sso_key`)，而是一个唯一字符串，每次调用 `refreshToken` 来刷新 `token` 或者调用 `logout` 注销 token 会默认把请求头中的 token 加入到黑名单，而不会影响到别的 token

> token 不做限制原理：token 不做限制，在 token 有效的时间内都能使用，你只要把配置文件中的 `blacklist_enabled` 设置为 `false` 即可，即为关闭黑名单功能

### 使用：

[](#使用)

##### 1、拉取依赖

[](#1拉取依赖)

> 使用 `Hyperf 2.x` 版本,则

```
composer require phper666/jwt-auth:~4.0.0
```

##### 2、发布配置

[](#2发布配置)

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

或者

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

##### 3、jwt配置

[](#3jwt配置)

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

```
