borzz.one Asked:2020-08-16 15:55:19 +0000 UTC2020-08-16 15:55:19 +0000 UTC 2020-08-16 15:55:19 +0000 UTC 在 api yii2 中检查令牌 772 如何在 Yii2 中自定义验证 rest api 令牌?Yii2 有 3 个标准类 QueryParamAuth HttpBasicAuth HttpBearerAuth 这些类检查用户表中的令牌,但我需要的不是授权,而只是我在 params.php 中编写的密钥。有这种可能吗? php 1 个回答 Voted Best Answer user411091 2021-10-19T12:40:03Z2021-10-19T12:40:03Z 仔细阅读文档 https://www.yiiframework.com/doc/guide/2.0/en/rest-authentication 之后,不用多想,我们只是从示例中重写了一段 class User extends ActiveRecord implements IdentityInterface { public static function findIdentityByAccessToken($token, $type = null) { //return static::findOne(['access_token' => $token]); return \Yii::$app->params['access_token'] == $token; } } 在文件config/params.php中添加参数 return [ ... 'access_token' => 'super-puper-security-key', ] 我希望这就是你要找的:)
仔细阅读文档 https://www.yiiframework.com/doc/guide/2.0/en/rest-authentication
之后,不用多想,我们只是从示例中重写了一段
在文件
config/params.php
中添加参数我希望这就是你要找的:)