CakePHP v.2.4...
我正在按照此文档尝试设置 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'
)
),
在我的用户控制器中::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
其他线索:我通过设置各种默认哈希算法(例如:"简单","河豚")和创建用户进行了实验。数据库中显示的哈希都是相同的,这告诉我我的配置设置被完全忽略了。
更新4
我在/lib/Cake/Controller/Component/Auth/BaseAuthenticate 的构造函数中调试了$this->settings
.php我的自定义哈希器设置在那里:
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"。
你可能会发现拥有PHPassHasherPasswordHaser的名字有点愚蠢,这取决于你想怎么称呼它。 也许PHPassPasswordHaser可能更合适一些(然后使用类名参数"PHPass")。
编辑:当一个接一个地使用多个大写字母时,似乎Cake有问题(例如。PHPass),所以正确的方法是将密码哈希类更改为以下内容:
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PhpassPasswordHasher extends AbstractPasswordHasher {
// functions
}
。并确保文件名与类名匹配:PhpassPasswordHasher.php。
感谢SDP的讨论,我今天学到了一些东西!
根据文档:
要为数组中的用户配置不同的字段$components请执行以下操作:
// Pass settings in $components array
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
)
)
);
源
我终于开始工作了。通过重命名文件/类以符合 Cake 约定,我们走上了正确的轨道。我不得不更进一步,也改变了大写:
PHPassPasswordHasher.php --> PhpassPasswordHasher.php
class PHPassPasswordHasher... --> class PhpassPasswordHasher...
唷!
ps:非常感谢希区柯克@Ben在这方面的支持。