Cakephp 2.x身份验证登录无效

时间:2014-04-09 09:25:07

标签: cakephp authentication encryption login

我开始创建一个表单来在我的数据库中创建用户/密码记录。我意识到创建的密码没有自动加密,因此我在创建用户记录时从互联网上获取了一些示例代码来加密我的密码。

问题是我的用户仍然无法登录。

这是User模型,代码应该是sha256密码(创建用户记录时)

public function beforeSave($options = array()) {
    if (!empty($this->data['User']['password'])) {
        $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
        $this->data['User']['password'] = $passwordHasher->hash(
            $this->data['User']['password']
        );
    }
    return true;
}

这似乎导致密码在数据库中保存为一串加密的乱码。因此我假设加密工作正常!

现在我们转到登录表单。

这是LoginsController登录代码,显示login.ctp并处理登录。

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash('Your username/password combination was incorrect');
        }
    }
}

这是我的login.ctp

<h2>Login</h2>

<?php

echo $this->Form->create();
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>

由于我在beforeSave函数中的早期用户模型中指定了sha256 hashtype,因此我还在我的AppController中添加了一个Auth组件&#39; authenticate&#39;用于指定&#39; hashType&#39;的数组=&GT; &#39; SHA256&#39 ;.我假设这指示Cakephp在用户尝试登录时使用哪种hashtype。

public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Auth'=>array(
        'loginRedirect'=>array('controller'=>'logins', 'action'=>'login'),  
        'logoutRedirect'=>array('controller'=>'logins', 'action'=>'logout'), 
        'authError'=>'You cannot access that page', //Error message whenever someone access a page without auth
        'authorize'=>array('Controller') //Where in our application that authorization will occur

    ),
    'authenticate' => array(
        'Form' => array(
            'passwordHasher' => array(
                'className' => 'Simple',
                'hashType' => 'sha256'
            )
        )
    )
);

我意识到我是否有这个&#39; hashType&#39; =&GT; &#39; SHA256&#39;在我的AppController中,登录不起作用。尝试登录时,始终会显示消息&#34;您的用户名/密码组合不正确&#34; 来自LoginsController登录操作。

问题

1)如何解决用户无法登录的问题?

2)为什么有很多方法(我在各种网站上找到)加密密码(例如使用别名,使用Security :: hash,以及使用SimplePasswordHasher等)

3)Auth组件预期的默认加密是什么?

谢谢!

3 个答案:

答案 0 :(得分:0)

你需要注意错误信息(它们通常很具描述性)和你的外壳(编码时也很重要)。

'authenticate' => array()!== 'Authenticate' => array()

后者是组件的名称,需要与其完全匹配。

关于2)有些人不赞成这样做。使用PasswordHasher是推荐的未来证明方法。

3)sha1(通过查看核心代码可以看到)cake2

PS:如果你很聪明,你现在正在使用cake3默认的河豚。 那么你们都为未来做好准备。

答案 1 :(得分:0)

我在AppController中将其更改为“Authenticate”但我仍然收到相同的错误消息。

我做的是从AppController中的Auth组件中删除了整个Authenticate数组,并将之前的用户模型替换为保存代码:

    if (isset($this->data[$this->alias]['password'])) {
        $passwordHasher = new SimplePasswordHasher();
        $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
        );
    }
    return true;

直接从Cakephp网站上获取此代码 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

现在我不确定正在使用什么样的散列方法,但它现在可以正常工作,创建用户现在可以登录。

答案 2 :(得分:0)

  
    

1)如何解决用户无法登录的问题?

  

首先支付参与错误消息。您收到错误Error: authenticateComponent could not be found.,因为您的$components数组无效。 authenticate密钥是Auth的设置,因此应该嵌套在Auth密钥的数组中。

  
    

2)为什么有很多方法(我在各种网站上找到)加密密码(例如使用别名,使用Security :: hash,以及使用SimplePasswordHasher等)

  

从2.4开始,正确的方法是使用书本示例中所示的SimplePasswordHasher。在此之前直接使用Security类。

  
    

3)Auth组件预期的默认加密是什么?

  

在2.x中,默认加密为sha1。由于您使用的是sha256,因此请确保db中的密码字段长度足以存储完整的哈希值。我不记得手头的长度是什么,你可以将字段类型设置为text以使事情顺利进行。