Can';在Cake php中使用AuthComponent登录
我想这是一个愚蠢的问题,但我登录到我正在构建的站点的受限部分时遇到了问题 在开始时,我看到Can';在Cake php中使用AuthComponent登录,php,cakephp,authentication,Php,Cakephp,Authentication,我想这是一个愚蠢的问题,但我登录到我正在构建的站点的受限部分时遇到了问题 在开始时,我看到$this->request->data['Usuario']['clave']中的密码字符串与在模型的beforeSave函数中使用SimplePasswordHasher的哈希字符串不同。我还应该说,该模型不是默认的用户模型,因为我正在用西班牙语编写应用程序,我不想使用默认模型,所以我对组件的配置是: class AppController extends Controller { /*func
$this->request->data['Usuario']['clave']
中的密码字符串与在模型的beforeSave
函数中使用SimplePasswordHasher
的哈希字符串不同。我还应该说,该模型不是默认的用户模型,因为我正在用西班牙语编写应用程序,我不想使用默认模型,所以我对组件的配置是:
class AppController extends Controller {
/*function beforeFilter() {
date_default_timezone_set('America/Mexico_City');
}*/
public $components = array(
'Session',
'Auth' => array(
'Form'=>array(
'userModel' => 'Usuario',
'unauthorizedRedirect' => false,
'loginRedirect' => array(
'controller' => 'ComplejosResidenciales',
'action' => 'index'
),
'logOutRedirect' => array(
'controller' => 'Usuarios',
'action' => 'index'
),
'fields' => array(
'username' => 'usuario',
'password' => 'clave'
),
'authorize' => 'Controller'
)
)
);
}
所以我决定不散列密码字段,但仍然没有用。
我希望有人能帮我一把,因为我是CakePHP的新手,不知道如何修复它
我想一定是Auth->login()
方法的问题,因为我在这里没有遵循惯例,但我不知道如何配置所述方法。目前情况如下:
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
return $this->Auth->redirectUrl($this->Auth->redirectUrl());
}
else {
$this->Session->setFlash(__('Las credenciales proporcionadas no son correctas'), 'default', array(), 'auth');
}
正如rrd指出的,我的$components数组是错误的,因此我将其更改为:
public $components = array(
'Session',
'Auth'=>array('loginRedirect'=>array('controller'=>'ComplejosResidenciales', 'action'=>'index'), 'logOutRedirect'=>array('controller'=>'Usuarios', 'action'=>'index'), 'loginAction'=>array('controller'=>'Usuarios', 'action'=>'login'), 'authenticate'=>array('Form'=>array('userModel'=>'Usuario', 'fields'=>array('username'=>'usuario', 'password'=>'clave')))));
根据cakephp.org,哪个更好
不要将其他身份验证配置密钥(如authError、loginAction等)放入authenticate或Form元素中。它们应该与身份验证密钥处于同一级别
但它不起作用
我一直在与它斗争,但我无法掌握它的窍门,我希望有人能指出我做错了什么。在我的AppController中,我声明了组件和beforeFilter函数,如下所示:
public $components = array('Auth'=>array('loginRedirect'=>array('controller'=>'ComplejosResidenciales', 'action'=>'index'),
'logoutRedirect'=>array('controller'=>'Usuarios', 'action'=>'login'),
), 'Session');
public function beforeFilter(){
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'Usuario', "fields" => array("username" => "usuario", "password" => "clave"), 'Form'));
}
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
return $this->Auth->redirectUrl($this->Auth->loginRedirect);
}
else {
$this->Session->setFlash(__('Las credenciales proporcionadas no son correctas'), 'default', array(), 'auth');
}
}
}
var $components = array('Auth', 'Session');
public function beforeFilter() {
$this->Auth->loginAction = array('controller'=>'Usuarios', 'action'=>'login');
$this->Auth->redirectLogin = array('controller'=>'ComplejosResidenciales', 'action'=>'add');
$this->Auth->authenticate = array('Form'=>array('userModel'=>'Usuario', 'fields'=>array('username'=>'usuario', 'password'=>'clave')));
}
然后我有一个登录函数,它在UsuariosController中运行(显然我猜是这样),如下所示:
public $components = array('Auth'=>array('loginRedirect'=>array('controller'=>'ComplejosResidenciales', 'action'=>'index'),
'logoutRedirect'=>array('controller'=>'Usuarios', 'action'=>'login'),
), 'Session');
public function beforeFilter(){
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'Usuario', "fields" => array("username" => "usuario", "password" => "clave"), 'Form'));
}
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
return $this->Auth->redirectUrl($this->Auth->loginRedirect);
}
else {
$this->Session->setFlash(__('Las credenciales proporcionadas no son correctas'), 'default', array(), 'auth');
}
}
}
var $components = array('Auth', 'Session');
public function beforeFilter() {
$this->Auth->loginAction = array('controller'=>'Usuarios', 'action'=>'login');
$this->Auth->redirectLogin = array('controller'=>'ComplejosResidenciales', 'action'=>'add');
$this->Auth->authenticate = array('Form'=>array('userModel'=>'Usuario', 'fields'=>array('username'=>'usuario', 'password'=>'clave')));
}
但我一直看到这样一个信息:“拉斯·克雷登夏莱斯·普罗西奥纳达斯没有儿子纠正”。我不知道我在$this->Auth->login()
部分中是否正确调用了Auth组件的方法,因为这样调用它显然没有结果,没有参数,但是我试着用参数$this->request->data
调用它,结果不管我在用户名和密码字段中写了什么,任何东西都会通过,当然这是不好的
现在我明白了为什么编码$this->Auth->login($this->request->data)
会使访问不受限制:
在2.x$this->Auth->login($this->request->data)中,用户将使用发布的任何数据登录,而在1.3$this->Auth->login($this->data)中,用户将首先尝试识别用户,并且仅在成功登录时登录
根据蛋糕的说明书。我似乎无法正确阅读有关这方面的任何文件。不管怎样,我请求有人能帮我,因为我在这里赶时间。在阅读了一些其他文档之后,我想只要我提供了正确的配置,Auth组件应该能够正确处理所有事情,因此我最终在AppController中执行了一个beforeFilter()
调用,如下所示:
public $components = array('Auth'=>array('loginRedirect'=>array('controller'=>'ComplejosResidenciales', 'action'=>'index'),
'logoutRedirect'=>array('controller'=>'Usuarios', 'action'=>'login'),
), 'Session');
public function beforeFilter(){
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'Usuario', "fields" => array("username" => "usuario", "password" => "clave"), 'Form'));
}
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
return $this->Auth->redirectUrl($this->Auth->loginRedirect);
}
else {
$this->Session->setFlash(__('Las credenciales proporcionadas no son correctas'), 'default', array(), 'auth');
}
}
}
var $components = array('Auth', 'Session');
public function beforeFilter() {
$this->Auth->loginAction = array('controller'=>'Usuarios', 'action'=>'login');
$this->Auth->redirectLogin = array('controller'=>'ComplejosResidenciales', 'action'=>'add');
$this->Auth->authenticate = array('Form'=>array('userModel'=>'Usuario', 'fields'=>array('username'=>'usuario', 'password'=>'clave')));
}
然后,在我的“UsuariosController”中,我做到了:
我有我的登录和注销功能,非常简单,但它不工作,它不会在登录时重定向我,也不会让我访问任何其他控制器,它似乎什么都不做。请帮忙
/**
* login and logout functions
*
*/
public function login() {
}
public function logout() {
$this->redirect($this->Auth->logout());
}
你应该改变更多的事情 $components应该是这样的:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username'=>'usuario', 'password'=>'clave')
)
)
)
);
我不确定用户模型,请查看手册
您应该实现isAuthorized
function isAuthorized($user){
if(in_array($this->action, array('view', 'edit')))
return true;
return false;
}
您的案例不需要beforeFilter
您的登录方法如下所示
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
同样,我建议您阅读上述手册或书。它有一个免费的样本,我想你会得到它的主要思想。因此,你应该清楚Auth的主要原则。阅读本手册或类似于Auth数组声明不正确的书籍。您使用的是什么版本的CakePHP?@rrd我使用的是cake 2.4.5,但我不知道我的组件声明有什么问题,我真的没有开发多少授权部分,但现在我只是尝试限制所有控制器,并在每个组件的beforeFilter部分中根据需要允许它们