我按照以下步骤实现 REST API 身份验证模块。由管理员创建用户 2。首先:通过基本身份验证登录以返回 access_token 3。在步骤 2 中使用 access_token 对用户进行身份验证。QueryParamAuth
作为该指令,它与 QueryParamAuth https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-authentication.md一起使用
但它在第 2 步不起作用。通过 BasicAuth 进行身份验证我对其进行了调试。$this->auth
总是返回 null。虽然$username
和$password
对
class HttpBasicAuth extends AuthMethod
/**
* @var callable a PHP callable that will authenticate the user with the HTTP basic auth information.
* The callable receives a username and a password as its parameters. It should return an identity object
* that matches the username and password. Null should be returned if there is no such identity.
*
* The following code is a typical implementation of this callable:
*
* ```php
* function ($username, $password) {
* return \app\models\User::findOne([
* 'username' => $username,
* 'password' => $password,
* ]);
* }
* ```
*
* If this property is not set, the username information will be considered as an access token
* while the password information will be ignored. The [[\yii\web\User::loginByAccessToken()]]
* method will be called to authenticate and login the user.
*/
public $auth;
public function authenticate($user, $request, $response)
{
$username = $request->getAuthUser();
$password = $request->getAuthPassword();
if ($this->auth) {
if ($username !== null || $password !== null) {
$identity = call_user_func($this->auth, $username, $password);
var_dump($identity);
die();
if ($identity !== null) {
$user->switchIdentity($identity);
} else {
$this->handleFailure($response);
}
return $identity;
}
} elseif ($username !== null) {
$identity = $user->loginByAccessToken($username, get_class($this));
if ($identity === null) {
$this->handleFailure($response);
}
return $identity;
}
return null;
}
我的问题是如何实现 $this->auth 功能?