我无法获取用户详细信息,因为$this->Auth->identify();
返回错误。我没有使用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);
}
}
这是我initialize()
在 AppController 中的:
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'passwordHasher' => [
'className' => 'Legacy',
]
]
]
]);
}
这是我的 login()UsersControllers
正在返回false
:
public function login() {
if($this->request->is('post')) {
$user = $this->Auth->identify();
var_dump($user);
exit();
}
}