身份验证登录在Cakephp中不起作用

身份验证登录在Cakephp中不起作用,cakephp,authentication,login,Cakephp,Authentication,Login,很抱歉再次提到这个话题,但是我已经搜索了关于这个话题的所有答案,但是没有找到解决方案(我对cakephp非常陌生): 我使用密码例程散列我的密码 在我的AppController中,我有: class AppController extends Controller { public $components = array('DebugKit.Toolbar','Session','Auth'); } public function add() { if ($this-

很抱歉再次提到这个话题,但是我已经搜索了关于这个话题的所有答案,但是没有找到解决方案(我对cakephp非常陌生): 我使用密码例程散列我的密码 在我的AppController中,我有:

class AppController extends Controller {
    public $components = array('DebugKit.Toolbar','Session','Auth');    
}
public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        // hash the password coming in from the form using Authcomponent::password
        $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);           
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

/** login method */
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                //redirect to page he was trying to access before login
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setflash('Invalid username or password');
            }
        }
    }
在我的UsersController中,我有:

class AppController extends Controller {
    public $components = array('DebugKit.Toolbar','Session','Auth');    
}
public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        // hash the password coming in from the form using Authcomponent::password
        $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);           
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

/** login method */
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                //redirect to page he was trying to access before login
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setflash('Invalid username or password');
            }
        }
    }
问题是添加用户后无法重新登录:我收到setflash消息。MySQL数据库上的密码哈希正确

感谢任何帮助:我不知道如何调试这个

编辑 我尝试过其他解决方案,从cakephp站点(没有成功)和2个youtube站点(没有成功)。我也尝试过普通密码和散列密码(使用默认密码和blowfish密码),结果都是一样的

我已将调试语句添加到代码中,如下所示:

public function login() {
     pr($this->request->data);      //debug
    if ($this->request->is('post')) {   //devbug
    echo ('post request');}             //debug
    if ($this->request->is('post')) {
debug($this->Auth->login());  //debug
debug($this->request->data);  //debug
            if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}
使用pr显示的数组($this->request->data);显示正确的数据,但是当我使用debug($this->request->data)时;密码中仅显示5个字符。这难道不是问题(或者说是转移注意力?) 结果如下所示:

Array
(
    [User] => Array
        (
            [username] => user
            [password] => password
        )

)

post request
\app\Controller\UsersController.php (line 18)

false

\app\Controller\UsersController.php (line 19)

array(
    'User' => array(
        'password' => '*****',
        'username' => 'user'
    )
)
你应该试试这个 应用控制器 用户控制器
尝试在登录函数中添加此行:

public function login() {
  pr($this->request->data);//LINE ADDED

if ($this->request->is('post')) {
    if ($this->Auth->login()) {
         return $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setflash('Invalid username or password');
    }
}

您将看到要传递给表单登录的数据。

您正在保存一个加密的密码,但当您登录软件时,需要一个未加密的密码。 尝试将一个未加密的密码放入数据库,它应该可以工作

在应用程序控制器中尝试以下操作:

public $components = array('DebugKit.Toolbar','Session','Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish',
            ),
        )
));

如果这仍然不起作用,请同时发布您的登录表单。

完全没有区别。这完全没有区别。也许我在代码中遗漏了什么?我怎样才能看到输入到Auth例程和从Auth例程中检索到的值?编辑:我已经在调试工具栏中查看了,并且Post数据正确地显示了我的最新用户名和密码,因此我认为它与Auth例程有关。请注意,我可以成功添加新用户,因此数据库连接良好。我再次尝试使用未加密的密码,但仍然无法验证。您的登录视图错误。您需要$this->Form->create('User');下次请提供所有代码…特别是当涉及到登录和填充时,我正在传递期望值:用户名'd'和密码pssord。我已经更改了代码,所以我存储了未加密的密码,但仍然失败。函数login(){//debug行跟随pr($this->request->data);if($this->request->is('post'){if($this->Auth->login()){$this->redirect($this->Auth->redirect());else{$this->Session setFlash(‘您的用户名或密码不正确’;}}}登录表单是:login