yiisoft/auth-jwt

Yii 身份验证 JWT 方法

2.1.0 2024-08-20 13:08 UTC

This package is auto-updated.

Last update: 2024-09-20 13:24:20 UTC


README

Yii

Yii 身份验证 JWT


Latest Stable Version Total Downloads Build status Code Coverage Mutation testing badge static analysis type-coverage psalm-level

该包为 JWT 身份验证 方法提供了对 Yii 身份验证 的支持。

要求

  • PHP 8.1 或更高版本。

安装

可以使用 Composer 安装此包

composer require yiisoft/auth-jwt

通用用法

在 Yii 中配置

  1. 在您的 params.php 配置文件中设置 JWT 参数

    'yiisoft/auth-jwt' => [
        'algorithms' => [
            // your signature algorithms
        ],
        'serializers' => [
            // your token serializers
        ],
        'key' => [
            'secret' => 'your-secret',
            'file' => 'your-certificate-file',
        ],
    ],
  2. 在配置文件中设置定义,例如在 config/web/auth.php 中,用于 \Yiisoft\Auth\Middleware\Authentication 中间件

    /** @var array $params */
    
    use Yiisoft\Auth\Jwt\TokenManagerInterface;
    use Yiisoft\Auth\Jwt\TokenManager;
    use Yiisoft\Auth\AuthenticationMethodInterface;
    use Yiisoft\Auth\Jwt\JwtMethod;
    
    return [
        KeyFactoryInterface::class => [
            'class' => FromSecret::class,
            '__construct()' => [
                $params['yiisoft/auth-jwt']['key']['secret']
            ],
        ],
        
        AuthenticationMethodInterface::class => JwtMethod::class,
    ];

    注意:不要忘记声明您的 \Yiisoft\Auth\IdentityInterface\Yiisoft\Auth\IdentityRepositoryInterface 实现。

  3. 使用 Yiisoft\Auth\Middleware\Authentication 中间件。有关中间件的更多信息,请参阅 中间件指南

独立配置

您可以手动配置 Authentication 中间件

/** @var \Yiisoft\Auth\IdentityRepositoryInterface $identityRepository */
$identityRepository = getIdentityRepository();

$tokenRepository = $container->get(\Yiisoft\Auth\Jwt\TokenRepositoryInterface::class);

$authenticationMethod = new \Yiisoft\Auth\Jwt\JwtMethod($identityRepository, $tokenRepository);

$middleware = new \Yiisoft\Auth\Middleware\Authentication(
    $authenticationMethod,
    $responseFactory, // PSR-17 ResponseFactoryInterface.
    $failureHandler // Optional, \Yiisoft\Auth\Handler\AuthenticationFailureHandler by default.
);

文档

如果您需要帮助或有问题,请访问 Yii 论坛。您还可以查看其他 Yii 社区资源

许可证

Yii Auth JWT 是免费软件。它根据 BSD 许可证发布。有关更多信息,请参阅 LICENSE

Yii 软件 维护。

支持项目

Open Collective

关注更新

Official website Twitter Telegram Facebook Slack