UserController.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shizhongqi
  5. * Date: 2019/11/19
  6. * Time: 22:43
  7. */
  8. namespace api\controllers;
  9. use Yii;
  10. use yii\rest\ActiveController;
  11. use \Lcobucci\JWT\Signer\Hmac\Sha256;
  12. class UserController extends ActiveController
  13. {
  14. public $modelClass = 'common\models\xhUser';
  15. public function actions()
  16. {
  17. $action= parent::actions(); // TODO: Change the autogenerated stub
  18. unset($action['index']);
  19. unset($action['create']);
  20. unset($action['update']);
  21. unset($action['delete']);
  22. }
  23. public function actionIndex()
  24. {
  25. return ['status'=>200, 'msg'=>'success'];
  26. }
  27. public function actionTest()
  28. {
  29. $builder = Yii::$app->jwt->getBuilder();
  30. $signer = new Sha256();
  31. $secret = "suspn@)!*";
  32. //设置header和payload,以下的字段都可以自定义
  33. $builder->setIssuer("suspn.com") //发布者
  34. ->setAudience("suspn.com") //接收者
  35. ->setId("abc", true) //对当前token设置的标识
  36. ->setIssuedAt(time()) //token创建时间
  37. ->setExpiration(time() + 60) //过期时间
  38. ->setNotBefore(time() + 5) //当前时间在这个时间前,token不能使用
  39. ->set('uid', 30061); //自定义数据
  40. //设置签名
  41. $builder->sign($signer, $secret);
  42. //获取加密后的token,转为字符串
  43. $token = (string)$builder->getToken();
  44. var_dump($token);
  45. }
  46. }