【问题标题】:CakePHP 2 Auth Not Working - BlowfishCakePHP 2 Auth 不工作 - Blowfish
【发布时间】:2016-10-24 15:21:51
【问题描述】:

我使用的是 CakePHP 2.8.5。它没有让我登录“用户名或密码不正确”。这在文档中似乎非常简单,但对我不起作用。我想知道我的模型/数据结构是否会混淆 CakePHP。我有一个 Users 模型,但登录名与 Admins 模型相关联。登录表单和操作位于 Pages 模型中(它具有多个模型的表单)。

在 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 中。 “电子邮件”是用户名字段:

<?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'));
        }
    }}

管理模型的顶部:

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;
}

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

如果你想看什么,我会添加它。

【问题讨论】:

  • “我注意到,如果我为不同的管理员输入相同的密码,我会得到不同的加密结果,但我读过这很正常。”这不正常。相同的密码应该生成相同的哈希。这就是哈希/比较的重点。如果它们不相同,则无法进行比较。
  • 对我来说就是这样,但后来我读到了这个问答:stackoverflow.com/questions/22667478/…
  • 我不确定他们在说什么,但对我来说似乎很糟糕。如果它的哈希每次都改变,你怎么可能比较一个密码。我每天都用蛋糕,从来没有遇到过这个问题。一定有什么事情发生。我也知道这是一样的,因为当我手动生成用户时,我有时会将现有的哈希复制/粘贴到新用户的密码字段中,而以前的密码可以正常工作。 (是的,不漂亮,但 w/e):)
  • The same password should generate the same hash @Dave 这在应用于河豚时是完全错误的。这是some random online tool 来演示。也是一个有用的参考php.net/manual/en/… - 请注意密码的哈希包含盐以验证哈希是否正确。
  • @AD7six - 我当然向您在这方面的专业知识低头(不是讽刺)。我认为我的逻辑是合理的,但显然其中存在差距。如何将散列密码字段从一个用户复制/粘贴到下一个用户,并为两者正确使用相同的密码功能?或者就此而言,如果每次输入的密码都不同,它如何将我输入的密码与数据库中的密码进行比较?

标签: authentication cakephp cakephp-2.0 blowfish


【解决方案1】:

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(或源代码中的文档块)错误更清楚:

...您可以使用“all”键定义应设置为所有身份验证对象的设置:

$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'
            )
        ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多