CakePHP 2.5 - 身份验证不起作用

时间:2014-07-29 12:19:41

标签: php cakephp authentication

关注tutorial我尝试创建简单身份验证,但我无法登录 - 始终收到消息“无效的用户名或密码,请重试”。我不明白为什么 - 用户名和密码是正确的。请帮忙找错。

我的模特

// app/Model/User.php
App::uses('AppModel', 'Model');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {
    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
            )
        )
    );

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

我的控制器

// app/Controller/UsersController.php
class UsersController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout');
    }

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            }
            $this->Session->setFlash(__('Invalid username or password, try again'));
        } 
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }
}

AppController.php

class AppController extends Controller {

    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'good',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'login',
                'action' => 'index',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => array(
                    'className' => 'Simple',
                    'hashType' => 'md5'
                    )
                )
            )
        )
    );

    public function beforeFilter() {
        $this->Auth->deny();
    }
}

我的观点(login.ctp)

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('username', array('label' => 'Username'));
        echo $this->Form->input('password', array('label' => 'Password'));
        ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

数据库表“users”:id,username,password(as md5)

1 个答案:

答案 0 :(得分:2)

  1. 对于AuthComponent,您已将SimplePasswordHasher配置为使用“md5”,但在您的beforeSave()回调中,您没有将其配置为使用md5(默认情况下使用sha1)。

  2. SimplePasswordHasher会在散列之前将安全盐附加到您的密码,因此如果您在用户表中手动添加了记录而没有对其进行记录,则无法使用。无盐md5散列非常弱。强烈建议不要使用它。但是如果你真的想要,你将不得不制作并使用一个自定义密码哈希类来生成没有盐的md5哈希。

相关问题