UserController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-08-30 18:10
  7. */
  8. namespace api\controllers;
  9. use yii\filters\auth\CompositeAuth;
  10. use yii\filters\auth\HttpBasicAuth;
  11. use yii\filters\auth\HttpBearerAuth;
  12. use yii\filters\auth\QueryParamAuth;
  13. use yii\helpers\ArrayHelper;
  14. use yii\filters\VerbFilter;
  15. /**
  16. * Class UserController
  17. * @package api\controllers
  18. *
  19. * 调用/register注册用户后,再次调用/login登录获取accessToken,再次访问/users?access-token=xxxxxxx访问
  20. */
  21. class UserController extends \yii\rest\ActiveController
  22. {
  23. public $modelClass = "api\models\User";
  24. public function behaviors()
  25. {
  26. return ArrayHelper::merge(parent::behaviors(), [
  27. 'authenticator' => [
  28. //使用ComopositeAuth混合认证
  29. 'class' => CompositeAuth::className(),
  30. 'optional' => [
  31. 'info',//无需access-token的action
  32. ],
  33. 'authMethods' => [
  34. HttpBasicAuth::className(),
  35. HttpBearerAuth::className(),
  36. [
  37. 'class' => QueryParamAuth::className(),
  38. 'tokenParam' => 'access-token',
  39. ]
  40. ]
  41. ],
  42. 'verbs' => [
  43. 'class' => VerbFilter::className(),
  44. 'actions' => [
  45. 'info' => ['GET'],
  46. ],
  47. ],
  48. ]);
  49. }
  50. public function actionInfo(){
  51. return ["我是user无需token可以访问的info"];
  52. }
  53. }