gpt4 book ai didi

php - Yii 2 UserIdentity findIdentityByAccessToken 没有被调用

转载 作者:可可西里 更新时间:2023-10-31 23:38:56 25 4
gpt4 key购买 nike

我正在 Yii 2 中创建一个 Web 应用程序,它使用 REST API 作为后端。使用 token 进行用户身份验证。

在初始身份验证期间,我正在执行以下操作:

$parsedUserDetails = [
'id' => $userDetails['user_id'],
'accessToken' => $userDetails['access_token'],
'email' => $this->email,
'password' => $this->password,
'authKey' => 'test'
];
$user = new Users($parsedUserDetails);
return Yii::$app->user->login($user, true ? 3600*24*30 : 0);

正在创建 cookie。

enter image description here

以下是我的 Users 类,它实现了 IdentityInterface

namespace app\models;

class Users extends \yii\base\Object implements \yii\web\IdentityInterface
{
public $id;
public $email;
public $password;
public $authKey;
public $accessToken;

/**
* @inheritdoc
*/
public static function findIdentity($id)
{

}

/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
exit('findIdentityByAccessToken');
}

/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}

/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}

/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}

/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
/*return $this->password === $password;*/
}
}

根据文档:http://www.yiiframework.com/doc-2.0/guide-security-authentication.html我应该将其余函数留空,findIdentityByAccessToken 函数将自动调用。不幸的是,findIdentityByAccessToken 没有被调用,而是 findIdentity 被调用。

我是不是做错了什么?

请帮忙

最佳答案

您必须在您的 User 类中覆盖 findIdentityByAccessToken() 并将您的 token 传递到稍后将验证的 header 中。

public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}

请在此处查看官方文档 http://www.yiiframework.com/doc-2.0/yii-web-identityinterface.html

但是,这将在您的用户表中搜索默认情况下不可用的字段access_token

您可以在 user 表中创建一个名为 access_token 的列,并创建逻辑来存储强创建的 key (使用加密或加盐技术),该 key 以后可以识别从您的 api 请求资源的用户。但这不会使您的 RESTful api 安全,即使使用 SSL。

HTTP Basic Auth: the access token is sent as the username. This should only be used when an access token can be safely stored on the API consumer side. For example, the API consumer is a program running on a server.

您可以使用 Oauth 2.0,它在安全性方面是一种更好的技术。阅读更多 here .

OAuth 2: the access token is obtained by the consumer from an authorization server and sent to the API server via HTTP Bearer Tokens, according to the OAuth2 protocol.

我找到了 this使用 Oauth 2.0 创建强大且安全的 RESTful Web 服务。但是,使用您的应用程序配置此扩展是一项有点棘手的任务。检查一下并尝试实现它。如果您发现使用它有困难,请尝试发布一个新问题。

希望对大家有帮助。

关于php - Yii 2 UserIdentity findIdentityByAccessToken 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32593316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com