damirka / yii2-jwt
简化JWT集成的特性
v0.2.1
2016-08-16 08:29 UTC
Requires
- firebase/php-jwt: ~3.0.0
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2024-09-16 21:10:00 UTC
README
为Yii2授权流程提供JWT实现
更多详情请参阅 JWT官方网站.
安装
要安装(目前只有master分支可用)运行
composer require "damirka/yii2-jwt:v0.2.1"
或者将此行添加到 composer.json 的 require 部分
"damirka/yii2-jwt": "v0.2.1"
用法
只有一个特性 - UserTrait - 它为 User 模型提供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() ] ]); } }
在 User 模型中
<?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 []; } // ... }