PHPackages                             uniondrug/token-auth-middleware - 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. uniondrug/token-auth-middleware

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

uniondrug/token-auth-middleware
===============================

Uniondrug Token Auth Middleware Component for uniondrug/framework

4.0.0(3y ago)01.8kproprietaryPHP

Since Apr 3Pushed 2y ago3 watchersCompare

[ Source](https://github.com/uniondrug/token-auth-middleware)[ Packagist](https://packagist.org/packages/uniondrug/token-auth-middleware)[ RSS](/packages/uniondrug-token-auth-middleware/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (0)

Token auth middleware component for uniondrug/framework
=======================================================

[](#token-auth-middleware-component-for-uniondrugframework)

安装
--

[](#安装)

```
$ cd project-home
$ composer require uniondrug/token-auth-middleware
```

> 本中间件依赖其他两个组件：`uniondrug/cache` 缓存服务，用于存储token, `uniondrug/middleware` 中间件基础组件。

修改 `app.php` 配置文件，加上Cache服务，服务名称`tokenAuthMiddleware`

```
return [
    'default' => [
        ......
        'providers'           => [
            ......
            \Uniondrug\TokenAuthMiddleware\TokenAuthServiceProvider::class,
        ],
    ],
];
```

配置
--

[](#配置)

配置文件在 `middleware.php` 中，需注册中间件，以及进行中间件配置。

```
return [
    'default' => [
        // 应用定义的中间件
        'middlewares' => [
            ...
            // 注册名为token的中间件
            'token' => \Uniondrug\TokenAuthMiddleware\TokenAuthMiddleware::class,
        ],

        // 将token中间件放在全局中间列表中
        'global'      => [
            'token', 'cors', 'cache', 'favicon', 'trace',
        ],

        ...

        // Token中间件的参数设置
        'token'       => [
            // 白名单，这个列表内的地址不需要认证，通常放登录接口等地址
            'whitelist' => [
                '/show'
            ],
            'ttl' => 7 * 86400, // 有效期7天，连续7天不登录将失效
        ],
    ],
];
```

`tokenAuthService` 服务的接口
------------------------

[](#tokenauthservice-服务的接口)

- `issueToken($userId, $username, $userMobile)`

颁发Token，在应用中为用户颁发Token，一般为登录成功后调用

- `revokeToken($tokenKey)`

注销Token，一般为在主动退出时调用

- `getUserId()`

返回经过认证的userid

- `getUsername()`

返回经过认证的username

TokenAuthMiddleware的一般使用说明
--------------------------

[](#tokenauthmiddleware的一般使用说明)

1、按上述步骤安装配置中间件；

2、发布登录认证接口，并且将登录接口设置为`whitelist`；

3、用户登录认证，认证成功后，调用`issueToken()`方法为该用户颁发token，并且将token返回给用户；

4、用户携带token，访问其他需要鉴权的接口；

5、在其他服务接口中，从请求头`X-UserId`和`X-Username`中获取用户身份信息；

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% 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 ~204 days

Recently: every ~38 days

Total

11

Last Release

970d ago

Major Versions

1.0.3 → 2.0.02020-04-13

2.0.0 → 3.0.02023-06-02

3.1.0 → 4.0.02023-06-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/865ceb7d8226574735eab8fc82de9cfbc4a708c291b3c816e75d53944348b686?d=identicon)[wsfuyibing](/maintainers/wsfuyibing)

---

Top Contributors

[![fuyibing](https://avatars.githubusercontent.com/u/8390620?v=4)](https://github.com/fuyibing "fuyibing (7 commits)")[![xueron](https://avatars.githubusercontent.com/u/2560873?v=4)](https://github.com/xueron "xueron (1 commits)")

---

Tags

middlewareauthtokenphalcon

### Embed Badge

![Health badge](/badges/uniondrug-token-auth-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/uniondrug-token-auth-middleware/health.svg)](https://phpackages.com/packages/uniondrug-token-auth-middleware)
```

###  Alternatives

[kreait/firebase-tokens

A library to work with Firebase tokens

23943.8M18](/packages/kreait-firebase-tokens)[adhocore/jwt

Ultra lightweight JSON web token (JWT) library for PHP5.5+.

3011.7M20](/packages/adhocore-jwt)[dyorg/slim-token-authentication

Slim 3.0+ Token Authentication Middleware

76109.8k](/packages/dyorg-slim-token-authentication)[erjanmx/laravel-api-auth

Dead simple Laravel api authorization middleware

1925.1k](/packages/erjanmx-laravel-api-auth)[matricali/akamai-token-auth

This library provides necessary logic to generate Akamai edge authorization token and signed URL.

111.2M](/packages/matricali-akamai-token-auth)[ingenerator/tokenista

Simple signed and expiring token generator and validator - for password reset, CSRF, authentication, whatever

1292.7k1](/packages/ingenerator-tokenista)

PHPackages © 2026

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