cakePHP电子邮件登录验证

时间:2013-09-15 18:44:01

标签: cakephp-2.3

您好我正在使用cakePHP版本2.3.6,我一直在尝试创建注册和登录,但是当我使用注册的用户名和密码登录时,它一直在说错误的用户名或密码,我也使用电子邮件用户名。请帮我谢谢。

AppController


 class AppController extends Controller {

        public $components = array(
        'Session',
        'Auth' => array(
           'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
           'logoutRedirect' => array('controller' => 'pages''action'                =>'display',  'home')
    )
);

    public function beforeFilter() {
        $this->Auth->allow('index', 'view');
    }


}


View Login

<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('email');
            echo $this->Form->input('password');
            ?>
    </fieldset>
    <?php echo $this->Form->end(__('Login')); ?>
</div>

UserController


<?php


class usersController extends AppController
{
    public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add','login','logout');
    }
    var $name = 'Users';

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
        throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
     }


    public function add()
    {

    if (!empty($this ->data))
    {
    $this->User->create();
    if ($this->User->save($this->data))
    {
        $this->Session->setFlash('Thank you for registering');
        $this->redirect(array('action'=>'index'));
       }

    }

    }




     function index()
    {

    }
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login())
            {
               $this->redirect($this->Auth->redirectUrl());
           }
            else
           {
               $this->Session->setFlash(__('Username or password is incorrect'),        'default', array(), 'auth');
       }
     }
    }
    public function logout() {
        return $this->redirect($this->Auth->logout());
    }
}

**UserModel**

App::uses('AuthComponent','Controller/Component');
    class User extends AppModel
{

        var $name = 'User';

    public $validate = array(
        'email' => array(
            'valid' => array(
            'rule' => 'email',
            'message' => 'Please enter an email address for username',

        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This username has already been taken',

        ),
        'eValid' => array(
            'required' => true,
                'rule' => array('notEmpty'),
                'message' => 'Please enter a username'

                )

        ),



        'password' => array(
        'pValid' => array(
        'required' => true,
        'rule' => array('notEmpty'),
        'message' => 'Please enter a valid password'
        ),

        'minPword' => array(
            'rule' => array('minLength', 8),
            'message' => 'Please enter a password that is minimum 8 characters'


        )

        ),


);








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


     }

1 个答案:

答案 0 :(得分:1)

CakePHP的Auth组件默认使用“用户名”和“密码”字段。首先,您必须告知Auth组件使用“email”字段而不是“username”字段。

'Auth' => array
(
       'authenticate' => array
       (
           'Form' => array
           (
               'fields' => array('username' => 'email', 'password' => 'password')
           )
       ),
       'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
       'logoutRedirect' => array('controller' => 'pages', 'action' =>'display', 'home')
)

我建议您激活isAuthorized检查,以便完全控制授权政策(http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization-objects)。请记住,您甚至可以设置自定义密码哈希方法以提高安全性。

快乐的编码!

修改

您的来源中存在多个问题:首先,转储中的密码字段对于密码哈希来说太短了...... 在CookBook的教程中,您将看到密码字段是VARCHAR(50 )而不是你的VARCHAR(30)。

其次,CakePHP是一个“约定优于配置”框架,所以总是记得按照食谱说明进行操作。控制器的名称必须是CamelCased,因此usersController必须是UsersController等等:这将帮助您避免使用

$name = 'Users';

语句。干式控制器&amp;较少的指令意味着较少的头痛;)并且尊重文件夹名称的约定!

第三,在你的SQL转储中,你的电子邮件有很多多条记录。这意味着即使解决了该字段的问题,您也可能在登录策略中发现了更多问题。幸运的是,您在使用模型验证进行一些测试后对此进行了修补...为了解决此问题,只需清空您的用户表并尝试一下。

第四,安全性改进:在我通过电子邮件发送给你的源代码中,我添加了这个

Security::setHash('sha256');

在您的登录()和添加()函数之上。这将为您的密码提供更强大的哈希值,并结合您的应用程序的盐。始终在所需的功能中声明这一点(即,保存或编辑用户的表密码字段的位置)。请记住,这需要编辑用户表的密码字段:将其更改为VARCHAR(64)或更高(实际上,SHA256返回64个字符串)。

就是这样,我真的希望你会喜欢CakePHP。

再次......快乐的编码!