用户名和密码无效,请登录

时间:2014-10-17 18:04:38

标签: cakephp

我正在使用Cakephp版本2.5.4

问题是我无法登录,$ this-> Auth-> login()每次都会返回false !!

相同的结果有或没有哈希密码,可能是什么错误? 花了一整天检查代码,但没办法..

如果您有时间查看此处的代码,我将不胜感激:https://gist.github.com/anonymous/2e0c6ab6b3b9a8604893

包括的所有内容:User.php,UsersController.php,login.ctp,register.ctp

提前感谢...

3 个答案:

答案 0 :(得分:2)

我不确定,但我认为cakephp 2.x中没有$this->data。您应该将$this->data更改为$this->request->data,并且所有字段都可供Auth。

使用

您在AppController中使用什么类和hashType?您需要在AppController和Model中使用相同的hashType。

实施例, App Controller:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => array(
                    'className' => 'Simple',
                    'hashType' => 'sha256'
                )
            )
        )
    )
);

型号:

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

答案 1 :(得分:1)

你的AppController中的

ok包括这个函数:

public function beforeFilter() {
   Security::setHash('sha1');

}

你说的是你的征兵是sha1

现在在你的数据库中你的字段'password'应该是VARCHAR(30)不超过30个字符。

而且我对register.ctp中的这一行并不感兴趣,并且有一个逗号更多,之后这是错误的

<?php echo $this->Form->create('User',array('type' => 'file','novalidate' => true,));?>

为什么是文件?我应该是这个

<?php echo $this->Form->create('User');?>

和novalidate可以在数据库中初始化为true,然后只升级到新值

确定。 我告诉你在AppController中添加了什么,现在将其添加到UserController中。当您查看表单以多次存储用户信息以使用sha1加密密钥时,会发生这种情况,这是我们在注册时存储在数据库中的加密。但是,当登录时可能会使用其他类型的加密发送此内容时通常不一样,而且当验证数据进行身份验证时比较a(sha1(密码注册)==其他加密(登录密码))是因为特定的beforeFilter在加密密钥中。

添加了此内容并更改了用户控制器:

  public function beforeFilter() {
         parent::beforeFilter();
  }
  public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                if($this->Auth->user('activated')==1){
                    $this->Session->setFlash(__('Bienvenido, '. $this->Auth->user('username')));
                     return $this->redirect($this->Auth->redirectUrl());
                }else{
                    $this->Session->setFlash(__('Your Email is not verified. Please verify and try again.'));
                    $this->redirect($this->Auth->logout());
                }
            } else {
                $this->Session->setFlash(__('Invalid Username or Password please try again'));
            }
        }
    }

答案 2 :(得分:0)

实际上是因为您可能没有使用密码哈希。您可以在用户模型中添加以下代码。

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

只需在您的模型中添加此代码。然后再次成为用户。如果您已在模型中添加此代码,则查看数据库。是否保存散列密码?如果是,则出现此错误的原因可能是另一个原因,即如果您在make user之后更改了安全盐。您可以通过创建新用户来解决此问题。现在问您将如何创建用户?

在你的appController中添加

$this->Auth->allow();

在你的beforeFilter()方法中。然后在你的浏览器中再次访问,你可以访问以建立一个新用户。让用户再次尝试登录。我认为它会起作用。