vaszloy / yii2-jwt
简化JWT集成的特性
v0.2.3
2022-03-16 15:25 UTC
Requires
- firebase/php-jwt: ^5.0
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2024-09-27 01:46:12 UTC
README
为Yii2授权流程实现JWT
详情请见 JWT官方网站.
安装
要安装(目前只有master分支可用),运行
composer require "vaszloy/yii2-jwt"
或者将此行添加到 composer.json 的 require 部分中
"vaszloy/yii2-jwt": "v5.0"
使用方法
只有一个特性 - UserTrait - 它为用户模型提供5个授权和JWT管理方法
设置
在控制器中
<?php // ... use yii\filters\auth\HttpBearerAuth; class BearerAuthController extends \yii\rest\ActiveController { public function behaviors() { return array_merge(parent::behaviors(), [ 'bearerAuth' => [ 'class' => HttpBearerAuth::className() ] ]); } }
在用户模型中
<?php // ... use yii\db\ActiveRecord; use yii\web\IdentityInterface class User extends ActiveRecord implements IdentityInterface { // Use the trait in your User model use \damirka\JWT\UserTrait; // Override this method protected static function getSecretKey() { return 'someSecretKey'; } // And this one if you wish protected static function getHeaderToken() { return []; } // ... }