由于$this->Auth->identify();
返回false,我无法获取用户详细信息。我没有使用DefaultPasswordHasher
。相反,我正在使用LegacyPasswordHasher
。
以下代码适用于LegacyPasswordHasher
:
<?php
namespace App\Auth;
use Cake\Auth\AbstractPasswordHasher;
use Cake\Utility\Security;
class LegacyPasswordHasher extends AbstractPasswordHasher
{
public static $hashType = null;
public function hash($string, $type = null, $salt = false) {
if (empty($type)) {
$type = static::$hashType;
}
$type = strtolower($type);
if ($type === 'blowfish') {
return static::_crypt($string, $salt);
}
if ($salt) {
if (!is_string($salt)) {
$salt = Security::salt();
}
$string = $salt . $string;
}
if (!$type || $type === 'sha1') {
if (function_exists('sha1')) {
return sha1($string);
}
$type = 'sha256';
}
if ($type === 'sha256' && function_exists('mhash')) {
return bin2hex(mhash(MHASH_SHA256, $string));
}
if (function_exists('hash')) {
return hash($type, $string);
}
return md5($string);
}
public function check($password, $hashedPassword)
{
return $this->hash($password) === $hashedPassword;
}
}
以下是用户实体中_setPassword
方法的代码:
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new LegacyPasswordHasher)->hash($password,null,true);
}
}
这是我在AppController中的initialize()
:
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'passwordHasher' => [
'className' => 'Legacy',
]
]
]
]);
}
这是UsersControllers
中我的登录名(),返回false
:
public function login() {
if($this->request->is('post')) {
$user = $this->Auth->identify();
var_dump($user);
exit();
}
}