CakePHP v.2.4...
我正在关注 this documentation尝试设置 Auth 组件以使用我的自定义密码哈希类:
App::uses('PHPassPasswordHasher', 'Controller/Component/Auth');
class AppController extends Controller {
// auth needed stuff
public $components = array(
'Session',
'Cookie',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username'=>'email', 'password'=>'password'),
'passwordHasher' => 'PHPass'
)
),
在我的 UsersController::login() 中,我调试了 $this->Auth->login();
的返回,它总是返回 false,即使我提交了正确的电子邮件/密码。
(注意:login()
不带任何参数对我来说很奇怪,但文档似乎暗示它会自动查看请求数据。如果我的配置不正确导致它检查 User.email 字段而不是用户名。)
提交的登录表单中的发布数据如下所示:
array(
'User' => array(
'password' => '*****',
'email' => 'whatever@example.com'
)
)
我错过了什么?
更新2
我开始怀疑使用的是默认哈希算法,而不是我的自定义类。我试图匹配文档中的示例,但他们对如何执行此操作非常模糊。
这是app/Controller/Component/Auth/PHPassPasswordHasher.php
的内容
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PHPassPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
$hasher = new new PasswordHash( 8, true );
return $hasher->HashPassword($password);
}
public function check($password, $hashedPassword) {
debug('PHPassHasher'); die('Using custom hasher'); //<--THIS NEVER HAPPENS!
$hasher = new new PasswordHash( 8, true );
return $hasher->CheckPassword($password, $hashedPassword);
}
}
啊哈! debug()
从未出现...所以我很确定问题出在我的自定义哈希器配置上。
更新3
其他线索:我通过设置各种默认哈希算法(例如:“Simple”、“Blowfish”)和创建用户进行了试验。数据库中显示的哈希值都是相同,这告诉我我的配置设置被完全忽略了。
更新4
我在/lib/Cake/Controller/Component/Auth/BaseAuthenticate.php 的构造函数中调试了 $this->settings
并且我的自定义哈希设置是 :
array(
'fields' => array(
'password' => 'password',
'username' => 'email'
),
'userModel' => 'User',
'scope' => array(),
'recursive' => (int) 0,
'contain' => null,
'passwordHasher' => 'PHPass'
)
最佳答案
您需要重命名您的密码哈希类以具有后缀“PasswordHasher”,并且仅在“className”参数中提供无后缀的名称。
例如:
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PHPassHasherPasswordHasher extends AbstractPasswordHasher {
// functions
}
文档中的示例将类名设置为“Simple”,然后加载“SimplePasswordHasher”。
您可能会发现将 PHPassHasherPasswordHasher 命名为有点傻,这取决于您想要如何调用它。也许 PHPassPasswordHasher 可能更合适(然后使用类名参数“PHPass”)。
编辑:似乎 Cake 在一个接一个地使用多个大写字母时出现问题(例如 PHPass),因此正确的方法是将密码哈希类更改为以下内容:
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PhpassPasswordHasher extends AbstractPasswordHasher {
// functions
}
...并确保文件名与类名匹配:PhpassPasswordHasher.php。
感谢 SDP 的讨论,我今天学到了一些东西!
关于php - 如何配置 CakePHP 的 $this->Auth->login() 以使用自定义密码哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643789/