CakePHP 2 Auth 不工作 - Blowfish

CakePHP 2 Auth Not Working - Blowfish

我正在使用 CakePHP 2.8.5。它不允许我登录 "Username or password is incorrect"。这在文档中看起来非常简单,但对我不起作用。我想知道我的 model/data 结构是否会混淆 CakePHP。我有一个用户模型,但登录名与管理员模型相关联。登录表单和操作在页面模型中(它有多个模型的表单)。

在 AppController 中:

public $components = array(
    'DebugKit.Toolbar',
    'Flash',
    'Session',
    'Auth' => array(
        'userModel' => 'Admin',
        'authenticate' => array(
            'Form' => array(
                'fields' => array(
                    'username' => 'email',
                    'password' => 'password'
                ),
                'passwordHasher' => 'Blowfish'
            )
        ),
        'loginAction' => array(
            'controller' => 'pages',
            'action' => 'login',
        ),
        'logoutRedirect' => array(
            'controller' => 'pages',
            'action' => 'login',
        ),
        'authError' => 'Please log in',
        'authorize' => array('Controller')
    )
);

我的登录视图,在 /View/Pages。 "email" 是用户名字段:

<?php
echo $this->Form->create('Admin'); 
echo $this->Form->input('email'); 
echo $this->Form->input('password'); 
echo $this->Form->end('Submit'); 
?>

页面控制器:

public function login() {

    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Flash->error(__('Username or password is incorrect'));
        }
    }}

Admin 模型顶部:

App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

Admin 模型中的自动 Blowfish 加密:

public function beforeSave($options = array()) {
    if (isset($this->data['Admin']['password'])) {
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data['Admin']['password'] = $passwordHasher->hash(
            $this->data['Admin']['password']
        );
    }
    return true;
}

我注意到如果我为不同的管理员输入相同的密码,我会得到不同的加密结果,但我已经看到这是正常的。

如果你还想看别的,我再补充。

userModel 密钥位置错误

比较问题中的配置:

public $components = array(
    'DebugKit.Toolbar',
    'Flash',
    'Session',
    'Auth' => array(
        'userModel' => 'Admin',
        'authenticate' => array(
            'Form' => array(
                'fields' => array(
                    'username' => 'email',
                    'password' => 'password'
                ),
                'passwordHasher' => 'Blowfish'
            )
        ),

到配置 in the docs:

$this->Auth->authenticate = array(
    'Basic' => array('userModel' => 'Member'),
    'Form' => array('userModel' => 'Member')
);

问题中 userModel 是顶级密钥,在文档中它是个人身份验证密钥的一部分。查看 api examples(或源代码中的文档块)错误更清楚:

... you can define settings that should be set to all authentications objects using the 'all' key:

$this->Auth->authenticate = array(
    'all' => array(
        'userModel' => 'Users.User',
        'scope' => array('User.active' => 1)
    ),
    'Form',
    'Basic'
);

可以为所有要使用的身份验证对象定义一个全局userModel,但语法与问题完全不同。

使用全部键

因此,要定义用于所有身份验证选项的用户模型,请使用 all 键:

public $components = array(
    'DebugKit.Toolbar',
    'Flash',
    'Session',
    'Auth' => array(
        //'userModel' => 'Admin', // <- no
        'authenticate' => array(
            'all' => array(
                'userModel' => 'Admin' // <- yes
            ),
            'Form' => array(
                'fields' => array(
                    'username' => 'email',
                    'password' => 'password'
                ),
                'passwordHasher' => 'Blowfish'
            )
        ),