AppapiTokenController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\app\appapi\modules;
  10. use Yii;
  11. use yii\filters\auth\CompositeAuth;
  12. use yii\filters\auth\HttpBasicAuth;
  13. use yii\filters\auth\HttpBearerAuth;
  14. use fecshop\yii\filters\auth\AppapiQueryParamAuth;
  15. use yii\web\Response;
  16. use yii\filters\RateLimiter;
  17. use fecshop\app\appapi\modules\AppapiController;
  18. /**
  19. * @author Terry Zhao <2358269014@qq.com>
  20. * @since 1.0
  21. */
  22. class AppapiTokenController extends AppapiController
  23. {
  24. public function behaviors()
  25. {
  26. $behaviors = parent::behaviors();
  27. $behaviors['authenticator'] = [
  28. 'class' => CompositeAuth::className(),
  29. 'authMethods' => [
  30. # 下面是三种验证access_token方式
  31. //HttpBasicAuth::className(),
  32. //HttpBearerAuth::className(),
  33. # 这是GET参数验证的方式
  34. # http://10.10.10.252:600/user/index/index?access-token=xxxxxxxxxxxxxxxxxxxx
  35. AppapiQueryParamAuth::className(),
  36. ],
  37. ];
  38. # rate limit部分,速度的设置是在
  39. # \myapp\code\core\Erp\User\models\User::getRateLimit($request, $action){
  40. /* 官方文档:
  41. 当速率限制被激活,默认情况下每个响应将包含以下HTTP头发送 目前的速率限制信息:
  42. X-Rate-Limit-Limit: 同一个时间段所允许的请求的最大数目;
  43. X-Rate-Limit-Remaining: 在当前时间段内剩余的请求的数量;
  44. X-Rate-Limit-Reset: 为了得到最大请求数所等待的秒数。
  45. 你可以禁用这些头信息通过配置 yii\filters\RateLimiter::enableRateLimitHeaders 为false, 就像在上面的代码示例所示。
  46. */
  47. $rateLimit = Yii::$app->params['rateLimit'];
  48. if(isset($rateLimit['enable']) && $rateLimit['enable']){
  49. $behaviors['rateLimiter'] = [
  50. 'class' => RateLimiter::className(),
  51. 'enableRateLimitHeaders' => true,
  52. ];
  53. }
  54. return $behaviors;
  55. }
  56. }