| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shizhongqi
- * Date: 2019/11/19
- * Time: 22:43
- */
- namespace api\controllers;
- use Yii;
- use yii\rest\ActiveController;
- use \Lcobucci\JWT\Signer\Hmac\Sha256;
- class UserController extends ActiveController
- {
- public $modelClass = 'common\models\xhUser';
- public function actions()
- {
- $action= parent::actions(); // TODO: Change the autogenerated stub
- unset($action['index']);
- unset($action['create']);
- unset($action['update']);
- unset($action['delete']);
- }
- public function actionIndex()
- {
- return ['status'=>200, 'msg'=>'success'];
- }
- public function actionTest()
- {
- $builder = Yii::$app->jwt->getBuilder();
- $signer = new Sha256();
- $secret = "suspn@)!*";
- //设置header和payload,以下的字段都可以自定义
- $builder->setIssuer("suspn.com") //发布者
- ->setAudience("suspn.com") //接收者
- ->setId("abc", true) //对当前token设置的标识
- ->setIssuedAt(time()) //token创建时间
- ->setExpiration(time() + 60) //过期时间
- ->setNotBefore(time() + 5) //当前时间在这个时间前,token不能使用
- ->set('uid', 30061); //自定义数据
- //设置签名
- $builder->sign($signer, $secret);
- //获取加密后的token,转为字符串
- $token = (string)$builder->getToken();
- var_dump($token);
- }
- }
|