PHPackages                             erikwang2013/jwt-webman - 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. erikwang2013/jwt-webman

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

erikwang2013/jwt-webman
=======================

erikwang2013/jwt-webman is a JWT plugin that is compatible with webman. Mainly suitable for distributed deployment, used to adapt to webman, with simple and fast installation.

v1.0.4(2w ago)026↓100%MITPHPPHP &gt;=7.2

Since Oct 8Pushed 2w agoCompare

[ Source](https://github.com/erikwang2013/jwt-webman)[ Packagist](https://packagist.org/packages/erikwang2013/jwt-webman)[ RSS](/packages/erikwang2013-jwt-webman/feed)WikiDiscussions main Synced today

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

简介
--

[](#简介)

erikwang2013/jwt-webman是一款适配webman的jwt插件。 主要是适用分布式部署，用于适配webman，安装简单快捷。

作者博客
----

[](#作者博客)

[艾瑞可erik](https://erik.xyz)

安装
--

[](#安装)

Use [Composer](https://github.com/composer/composer):

```
composer require erikwang2013/jwt-webman
```

使用示例
----

[](#使用示例)

```

use ErikJwt\Config;
use ErikJwt\JWTFactory;
use ErikJwt\JWTException;

try {
    // 创建JWT实例
    $jwt = JWTFactory::createFromConfig();

    // 生成令牌
    $token = $jwt->encode(['user_id' => 123, 'username' => 'testuser']);
    echo "Token generated: " . substr($token, 0, 50) . "...\n";

    //生成刷新令牌
    $refreshToken = $jwt->encode([
        'user_id' => 123,
        'token_type' => 'refresh'
    ], 86400); // 24小时过期

    // 验证令牌
    $payload = $jwt->decode($token);

    echo "Token validated for user: " . $payload['username'] . "\n";

    //验证令牌状态
    $jwt->validate($token);

    // 将令牌加入黑名单
    $jwt->blacklist($token);
    echo "Token blacklisted\n";

    // 检查黑名单
    if ($jwt->isBlacklisted($token)) {
        echo "Token correctly identified as blacklisted\n";
    }

} catch (JWTException $e) {
    // 处理不同类型的异常
    switch ($e->getCode()) {
        case JWTException::STORAGE_ERROR:
            echo "Storage error: " . $e->getMessage() . "\n";
            // 可以回退到文件存储
            $fallbackConfig = new Config([
                'secret_key' => 'your-secret-key',
                'storage' => ['type' => 'file']
            ]);
            $jwt = JWTFactory::createFromConfig($fallbackConfig);
            echo "Fallback to file storage\n";
            break;

        case JWTException::NETWORK_ERROR:
            echo "Network error: " . $e->getMessage() . "\n";
            // 记录日志，通知管理员等
            break;

        case JWTException::CONFIG_ERROR:
            echo "Configuration error: " . $e->getMessage() . "\n";
            // 检查配置文件
            break;

        default:
            echo "JWT error: " . $e->getMessage() . "\n";
            break;
    }
} catch (Exception $e) {
    echo "Unexpected error: " . $e->getMessage() . "\n";
}

// 优雅降级示例
function createJWTWithFallback(array $configs): \ErikJwt\JWT
{
    $lastException = null;

    foreach ($configs as $config) {
        try {
            return JWTFactory::createFromConfig(new \ErikJwt\Config($config));
        } catch (JWTException $e) {
            $lastException = $e;
            // 继续尝试下一个配置
            continue;
        }
    }

    // 所有配置都失败，抛出最后一个异常
    throw $lastException;
}

// 使用多个存储后端配置
$configs = [
    [
        'secret_key' => 'your-secret-key',
        'storage' => [
            'type' => 'redis',
            'config' => [
                'database' => 1,
                'prefix' => 'prod:jwt:blacklist:',
                'timeout' => 1.0,
                'read_timeout' => 1.0,
                'persistent' => true,
                'persistent_id' => 'jwt_pool'
            ]
        ]
    ],
    [
        'secret_key' => 'your-secret-key',
        'storage' => [
            'type' => 'database',
            'config' => [
                'table_name' => 'user_token_blacklist',
            ]
        ]
    ],
    [
        'secret_key' => 'your-secret-key',
        'storage' => ['type' => 'file']
    ]
];

try {
    $jwt = createJWTWithFallback($configs);
    echo "JWT instance created successfully with fallback\n";
} catch (Exception $e) {
    echo "All storage backends failed: " . $e->getMessage() . "\n";
}

```

配置文件
----

[](#配置文件)

仅供参考，根据实际配置。

- file

```
