在cakephp 2.4中调试AuthComponent

时间:2014-04-13 00:49:06

标签: cakephp

我用users表烘焙了一个cakephp应用程序,我正在尝试使用Blowfish哈希进行身份验证。我的密码字段是varchar(255),所以它应该足够长以存储哈希。应用程序中的所有内容都是默认的烘焙输出,预期如下。

这个问题是我在创建用户后无法登录;我总是得到“拒绝访问”。对此进行故障排除的最佳方法是什么?

AppController.php

App::uses('Controller', 'Controller');

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

    public $components = array(
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email'),
                    'passwordHasher' => 'Blowfish'
                    )
                ),
            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
            'authError' => "Access Denied",
            'authorize' => array('Controller'),
        )
    );

    public function isAuthorized($user){
        return true;
    }
}

User.php(模特)

App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

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

UsersController.php

public function login(){
    if ($this->request->is('post')) {
        if($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        }
        else {
            $this->Session->setFlash('Access Denied');
        }
    }
}

login.ctp

echo $this->Form->create('user');
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->button('Log In', array('type' => 'submit');
echo $this->Form->end();

“调试($这 - >请求);死;'在登录功能中给出以下输出。密码应该是 * 还是应该是输入的哈希版本?

data => array(
    'user' => array(
        'password' => '*****',
        'email' => 'test@test.com'
    )
)

2 个答案:

答案 0 :(得分:2)

1)听@waspinator echo $this->Form->create('User');

2)

App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

删除广告将其放在AppController中,它应该是

App::uses('AuthComponent', 'Controller/Component');

3)评论这一行

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

//public function isAuthorized($user){
//        return true;
//}

4)第一次将其置于用户控制器之上,以便保存密码

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

答案 1 :(得分:1)

echo $this->Form->create('user');

应该是

echo $this->Form->create('User');
相关问题