使用DiegstAuthenticate登录CakePHP不起作用
我使用的是CakePHP2.3.6。在一个项目中,我必须为使用DiegstAuthenticate登录CakePHP不起作用,cakephp,authentication,Cakephp,Authentication,我使用的是CakePHP2.3.6。在一个项目中,我必须为管理员面板和学生面板实现用户管理系统。首先,我想我可以在一个控制器中安装一个login&logout系统,比如userscoontroller。但是,后来我发现我需要两个不同的登录页面用于两个面板,这就是为什么我在学生控制器和管理员控制器中放置了两个登录函数的原因 接下来,首先,我使用会话实现了完整的用户管理系统,这意味着使用会话手动登录和注销用户。那很好。但是,最近我想,如果API中已经有代码,为什么要手动编写代码呢?因此,现在我尝试使
管理员
面板和学生
面板实现用户管理系统
。首先,我想我可以在一个控制器中安装一个login&logout
系统,比如userscoontroller
。但是,后来我发现我需要两个不同的登录页面
用于两个面板,这就是为什么我在学生控制器
和管理员控制器
中放置了两个登录
函数的原因
接下来,首先,我使用会话
实现了完整的用户管理系统
,这意味着使用会话
手动登录和注销用户。那很好。但是,最近我想,如果API中已经有代码,为什么要手动编写代码呢?因此,现在我尝试使用AuthComponent
来实现这一点
但是,当我将所有内容从Session
更改为AuthComponent
时,它的行为很奇怪。以下是AuthComponent
的代码:
在学生控制器中
:
public $components=array('Auth'=>array(
'loginRedirect'=>array('controller'=>'students','action'=>'editProfile'),
'logoutRedirect'=>array('controller'=>'students','action'=>'index'),
'authenticate'=>array('Digest'=>array('scope'=>array('User.role'=>"student"))),
'unauthorizedRedirect'=>array('controller'=>'students','action'=>'login')
)
);
public $components=array('Auth'=>array(
'loginRedirect'=>array('controller'=>'admins','action'=>'index'),
'logoutRedirect'=>array('controller'=>'admins','action'=>'index'),
'authenticate'=>array('Digest'=>array('scope'=>array('User.role'=>"admin"))),
'unauthorizedRedirect'=>array('controller'=>'admins','action'=>'login')
)
);
在beforeFilter
功能中:
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login','signUp');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Student->field('name',array('Student.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Admin->field('name',array('Admin.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
在登录
功能中:
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login','signUp');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Student->field('name',array('Student.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Admin->field('name',array('Admin.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
同样,在管理员控制器中:
public $components=array('Auth'=>array(
'loginRedirect'=>array('controller'=>'students','action'=>'editProfile'),
'logoutRedirect'=>array('controller'=>'students','action'=>'index'),
'authenticate'=>array('Digest'=>array('scope'=>array('User.role'=>"student"))),
'unauthorizedRedirect'=>array('controller'=>'students','action'=>'login')
)
);
public $components=array('Auth'=>array(
'loginRedirect'=>array('controller'=>'admins','action'=>'index'),
'logoutRedirect'=>array('controller'=>'admins','action'=>'index'),
'authenticate'=>array('Digest'=>array('scope'=>array('User.role'=>"admin"))),
'unauthorizedRedirect'=>array('controller'=>'admins','action'=>'login')
)
);
在beforeFilter
功能中:
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login','signUp');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Student->field('name',array('Student.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Admin->field('name',array('Admin.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
在登录
功能中:
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login','signUp');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Student->field('name',array('Student.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','login');
$this->Auth->deny('editProfile');
$this->layout='applicant_layout';
}
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$this->Session->setFlash('Welcome '.$this->Admin->field('name',array('Admin.id'=>$this->Auth->user('id'))));
$this->set('title_for_layout','Login');
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
在这里,我只在用户
数据库表中存储登录凭据(id
,用户名
,密码
,角色
,创建
,修改
)
我的第一个问题是:对于两个面板,AdminsController
&StudentsController
,是否可以在一个控制器中有一个登录/注销部分,比如说UsersController
?怎样我只想在userscoontroller
中为两个面板提供一个登录和注销功能,其中每个面板都有不同的登录页面,为AuthComponent(loginDirect、logoutRedirect、scope等)提供不同的配置
我的第二个问题是:当我使用DigestAuthenticate
时,当我尝试从自己的登录页面登录时,会出现另一个登录对话框。该对话框不接受任何用户名/密码。我不想要那个盒子,我想要简单的登录系统,没有任何复杂性。我只想使用digest,因为它更安全
当我注销或转到未经身份验证的用户不允许的任何页面(功能)时,它会说我应该有一个userscocontroller
,具有login
功能。但是,现在我为两个面板提供了不同的登录功能
那么,解决方案是什么?我应该回到基于会话的登录/注销系统,还是应该使用摘要?我还尝试使用表单
身份验证,但结果相同,它在用户
控制器中查找登录
函数
请帮帮我
谢谢我发现这很有帮助,为什么这里没有人回应?这不是一个重要的问题,还是没有人知道解决方案?