ActiveController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace api\controllers;
  3. use common\behaviors\HttpSignAuth;
  4. use common\models\mall\Mall;
  5. use common\traits\BaseAction;
  6. use common\traits\BaseControllerTrait;
  7. use Yii;
  8. use yii\filters\auth\CompositeAuth;
  9. use yii\filters\auth\HttpBasicAuth;
  10. use yii\filters\auth\HttpBearerAuth;
  11. use yii\filters\auth\HttpHeaderAuth;
  12. use yii\filters\RateLimiter;
  13. use yii\web\BadRequestHttpException;
  14. /**
  15. *
  16. * Class ActiveController
  17. * @package api\controllers
  18. * @author qimall
  19. */
  20. class ActiveController extends \yii\rest\ActiveController
  21. {
  22. use BaseAction;
  23. use BaseControllerTrait;
  24. public $tokenParam = 'access-token';
  25. /**
  26. * token 验证的key
  27. *
  28. * @var string
  29. */
  30. protected $authMethodsHeader = 'x-access-token';
  31. /**
  32. * User验证类
  33. *
  34. * @var HttpHeaderAuth
  35. */
  36. protected $authMethodsClass = HttpHeaderAuth::class;
  37. /**
  38. * 不用进行登录验证的方法
  39. * 例如: ['index', 'update', 'create', 'view', 'delete']
  40. * 默认全部需要验证
  41. *
  42. * @var array
  43. */
  44. protected $authOptional = [];
  45. /**
  46. * 不用进行签名验证的方法
  47. * 例如: ['index', 'update', 'create', 'view', 'delete']
  48. * 默认全部需要验证
  49. *
  50. * @var array
  51. */
  52. protected $signOptional = [];
  53. /**
  54. * 行为验证
  55. *
  56. * @return array
  57. */
  58. public function behaviors()
  59. {
  60. $behaviors = parent::behaviors();
  61. // 进行签名验证
  62. if (Yii::$app->params['user.httpSignValidity'] == true) {
  63. $behaviors['signTokenValidate'] = [
  64. 'class' => HttpSignAuth::class,
  65. 'optional' => $this->signOptional, // 不进行认证判断方法
  66. ];
  67. }
  68. $behaviors['authenticator'] = [
  69. 'class' => CompositeAuth::class,
  70. 'authMethods' => [
  71. /**
  72. * 下面是四种验证access_token方式
  73. *
  74. * 1.HTTP 基本认证: access token 当作用户名发送,应用在access token可安全存在API使用端的场景,例如,API使用端是运行在一台服务器上的程序。
  75. * \yii\filters\auth\HttpBasicAuth::class,
  76. *
  77. * 2.OAuth : 使用者从认证服务器上获取基于OAuth2协议的access token,然后通过 HTTP Bearer Tokens 发送到API 服务器。
  78. * header格式:Authorization:Bearer+空格+access-token
  79. * yii\filters\auth\HttpBearerAuth::class,
  80. *
  81. * 3.请求参数 access token 当作API URL请求参数发送,这种方式应主要用于JSONP请求,因为它不能使用HTTP头来发送access token
  82. * http://rageframe.com/user/index/index?access-token=123
  83. *
  84. * 4.请求参数 access token 当作API header请求参数发送
  85. * header格式: x-api-key: access-token
  86. * yii\filters\auth\HttpHeaderAuth::class,
  87. */
  88. // HttpBasicAuth::class,
  89. // HttpBearerAuth::class,
  90. [
  91. 'class' => $this->authMethodsClass,
  92. 'header' => $this->authMethodsHeader,
  93. ],
  94. // [
  95. // 'class' => QueryParamAuth::class,
  96. // 'tokenParam' => 'access-token',
  97. // ],
  98. ],
  99. // 不进行认证判断方法
  100. 'optional' => $this->authOptional,
  101. ];
  102. /**
  103. * 请求速率控制
  104. *
  105. * limit部分,速度的设置是在common\models\common\RateLimit::getRateLimit($request, $action)
  106. * 当速率限制被激活,默认情况下每个响应将包含以下HTTP头发送 目前的速率限制信息:
  107. * X-Rate-Limit-Limit: 同一个时间段所允许的请求的最大数目;
  108. * X-Rate-Limit-Remaining: 在当前时间段内剩余的请求的数量;
  109. * X-Rate-Limit-Reset: 为了得到最大请求数所等待的秒数。
  110. * enableRateLimitHeaders:false: 不开启限制 true:开启限制
  111. */
  112. $behaviors['rateLimiter'] = [
  113. 'class' => RateLimiter::class,
  114. 'enableRateLimitHeaders' => false,
  115. ];
  116. // 行为日志
  117. // $behaviors['actionLog'] = [
  118. // 'class' => ActionLogBehavior::class,
  119. // ];
  120. return $behaviors;
  121. }
  122. // /**
  123. // * {@inheritdoc}
  124. // */
  125. // protected function verbs()
  126. // {
  127. // return [
  128. // 'index' => ['GET', 'HEAD', 'OPTIONS'],
  129. // 'view' => ['GET', 'HEAD', 'OPTIONS'],
  130. // 'create' => ['POST', 'OPTIONS'],
  131. // 'update' => ['PUT', 'PATCH', 'OPTIONS'],
  132. // 'delete' => ['DELETE', 'OPTIONS'],
  133. // ];
  134. // }
  135. /**
  136. * 前置操作验证token有效期和记录日志和检查curd权限
  137. *
  138. * @param $action
  139. * @return bool
  140. * @throws BadRequestHttpException
  141. * @throws \yii\base\InvalidConfigException
  142. * @throws \yii\web\ForbiddenHttpException
  143. */
  144. public function beforeAction($action)
  145. {
  146. if (!parent::beforeAction($action)) {
  147. return false;
  148. }
  149. // 权限方法检查,如果用了rbac,请注释掉
  150. $this->checkAccess($action->id, $this->modelClass, Yii::$app->request->get());
  151. // 每页数量
  152. $this->pageSize = Yii::$app->request->get('per-page', 10);
  153. $this->pageSize > 50 && $this->pageSize = 50;
  154. return true;
  155. }
  156. }