YII2-用户脱发 - 如何哈希密码



我想执行哈希密码,并使用数据库(password_hash)检查我该怎么做???

        $username = $auth['username'];

我的密码是

 $password = $auth['password'];

我想要哈希:

 $find = dektriumusermodelsUser::findOne(['username' => $username, 'password_hash' => $password]);

您可以使用

生成$哈希
$hash = Yii::$app->getSecurity()->generatePasswordHash($password);

$find = dektriumusermodelsUser::findOne(['username' => $username, 
      'password_hash' => $hash]);

下面的代码来自dektrium/yii2-user/helper/password.php(hash函数的代码。

public static function hash($password)
{
    return Yii::$app->security->generatePasswordHash($password,
      Yii::$app->getModule('user')->cost);
}

默认成本= 8

我知道很晚要回答这个问题,但是对于那些仍在寻找的人。我最近遇到了这个问题,在下面的大量测试之后,代码对我有用:

$behaviors['authenticator'] = [
        'class' => HttpBasicAuth::className(),
        'auth' => function ($username, $password) {
            $user = dektriumusermodelsUser::findOne(['username' => $username]);
            if ($user->validate($password)) {
                return $user;
            }
            return null;
        }
    ];

最新更新