yiisoft / auth-jwt
Yii 身份验证 JWT 方法
2.1.0
2024-08-20 13:08 UTC
Requires
- php: ^8.1
- psr/http-message: ^1.0|^2.0
- web-token/jwt-library: ^3.3
- yiisoft/auth: ^3.0
- yiisoft/http: ^1.2
- yiisoft/json: ^1.0
Requires (Dev)
- maglnet/composer-require-checker: ^3.8|^4.2
- nyholm/psr7: ^1.3
- phpunit/phpunit: ^9.5
- rector/rector: ^1.0.0
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.21
- yiisoft/di: ^1.1
- yiisoft/injector: ^1.1
README
Yii 身份验证 JWT
该包为 JWT 身份验证 方法提供了对 Yii 身份验证 的支持。
要求
- PHP 8.1 或更高版本。
安装
可以使用 Composer 安装此包
composer require yiisoft/auth-jwt
通用用法
在 Yii 中配置
-
在您的
params.php
配置文件中设置 JWT 参数'yiisoft/auth-jwt' => [ 'algorithms' => [ // your signature algorithms ], 'serializers' => [ // your token serializers ], 'key' => [ 'secret' => 'your-secret', 'file' => 'your-certificate-file', ], ],
-
在配置文件中设置定义,例如在
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
实现。 -
使用
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 软件 维护。