CakePHP 2.3.1身份验证组件
我最近一直在讨论一些关于Auth组件的CakePHP教程,并将其作为参考: 我的蛋糕版本是2.3.1 我正在使用Firebug进行调试。数据库密码是散列的权利,我可以注册和新用户刚刚好,但登录是一个不同的故事。当我尝试访问需要身份验证的页面时,我会获得登录页面,输入有效的用户/密码,然后它会将我重定向回登录页面。在firebug中,POST标题是200 OK,只需再次返回登录页面。我猜我的会话配置可能有问题,好像没有查找会话。这是我的UsersController.php:CakePHP 2.3.1身份验证组件,php,cakephp,authentication,Php,Cakephp,Authentication,我最近一直在讨论一些关于Auth组件的CakePHP教程,并将其作为参考: 我的蛋糕版本是2.3.1 我正在使用Firebug进行调试。数据库密码是散列的权利,我可以注册和新用户刚刚好,但登录是一个不同的故事。当我尝试访问需要身份验证的页面时,我会获得登录页面,输入有效的用户/密码,然后它会将我重定向回登录页面。在firebug中,POST标题是200 OK,只需再次返回登录页面。我猜我的会话配置可能有问题,好像没有查找会话。这是我的UsersController.php: <?php c
<?php
class UsersController extends AppController {
public $name = 'Users';
public $helpers = array('Html','Form');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'login');
}
public function login() {
//if already logged-in, redirect
if($this->Session->check('Auth.User')){
$this->redirect(array('controller'=>'admin','action' => 'admin_index'));
}
// if we get the post information, try to authenticate
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password'));
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
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 ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'login'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
}
}
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
public function delete($id = null) {
$this->request->onlyAllow('post');
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->User->delete()) {
$this->Session->setFlash(__('User deleted'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('User was not deleted'));
return $this->redirect(array('action' => 'index'));
}
}
?>
我的模型似乎工作正常,但如果这不是我的错误所在,我可以提供任何额外的代码!谢谢大家 问题在于密码的加密,请查看您是否正在进行beforesave,并且在保存时需要加密您登录的密码,以验证它们在数据库中是否相同 用户模型 应用控制器 在数据库中,密码的右边是varchar 30
public function beforeSave($options = array()) {
// hash our password
if (!$this->id) {
$passwordHasher = new SimplePasswordHasher();
$this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
}
return true;
}
public function beforeFilter() {
Security::setHash('sha1');
$this->Auth->allow('index', 'display', 'view');
}