使用 Cakephp 进行 LDAP 身份验证 3.x
LDAP Authentication with Cakephp 3.x
我是 Cakephp 的新手,现在正致力于在我的应用程序中实现 LDAP 身份验证。官方网站上 "bookmarker" 教程中的很多东西都是自动运行的,所以它没有给我足够的信息来说明如何实现一些特定的身份验证。我已经检查过这个 post 并尝试以这种方式实现我的身份验证,但仍然存在一些理解问题。
在我的数据库中,我有一个 table "Students",它有一个属性 "id" 作为主键。
我的 AppController 如下所示:
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Auth', ['authenticate' =>
['Form' =>
['fields' =>
['username' => 'email',
'password' => 'password']
]
], 'loginAction' => [
'controller' => 'Students',
'action' => 'login'
]
]);
$this->Auth->config('authenticate', ['Ldap']);
}
public function isAuthorized()
{
if($this->Auth->user('departmentnumber') == "***") { return true; }
}
LdapAuthenticate class 就像我上面提到的 post 中的一样:
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Network\Request;
use Cake\Network\Response;
class LdapAuthenticate extends BaseAuthenticate {
protected $_host = '***' ;
public function authenticate(Request $request, Response $response) {
$username = $request->data['username'] ;
$password = $request->data['password'] ;
$ds = @ldap_connect($this->_host) ;
if (!$ds) {
throw \Cake\Error\FatalErrorException ('Unable to connect to LDAP host.') ;
}
$basedn = "cn=Users,dc=***";
$dn = "cn=$username, " . $basedn;
$ldapbind = @ldap_bind($ds, $dn, $password);
if (!$ldapbind) {
return false ;
}
$entry = ldap_first_entry ($ldapbind) ;
$attrs = ldap_get_attributes ($ldapbind, $entry) ;
$user = [] ;
// Loop
for ($i = 0 ; $i < $attrs["count"] ; $i++) {
$user[$attrs[$i]] = ldap_values ($ldapbind, $entry, $attrs[$i])[0] ;
}
// Then close it and return the authenticated user
ldap_unbind ($ldapbind) ;
return $user ;
}
}
在 StudentsController 中,我已经像 "bookmarker" 教程中那样实现了登录和注销功能:
public function login(){
if ($this->request->is('post')){
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
// user is not identified
$this->Flash->error('Your username or password is not correct');
}
}
public function logout(){
$this->Flash->success('You are now logged out');
return $this->redirect($this->Auth->logout());
}
当我打开任何页面时,我都成功地登陆了我的 login.ctp 页面。在我输入我的凭据并单击 "login" 后,我收到一个错误 "SQLSTATE[42S02]: Base table or view not found: 1146 Table '***_db.users' doesn't exist"。
所以我想我做错了什么,但没有足够的理解来找到哪里——不知道为什么它试图在我的数据库中找到一个不存在的 "users" table。
感谢大家提前帮我出出主意!
由于配置不正确。不要在您的身份验证配置中使用 Form
,而是使用 Ldap
:
$this->loadComponent('Auth', ['authenticate' =>
['Ldap' =>
['fields' =>
['username' => 'email',
'password' => 'password']
]
], 'loginAction' => [
'controller' => 'Students',
'action' => 'login'
]
]);
我是 Cakephp 的新手,现在正致力于在我的应用程序中实现 LDAP 身份验证。官方网站上 "bookmarker" 教程中的很多东西都是自动运行的,所以它没有给我足够的信息来说明如何实现一些特定的身份验证。我已经检查过这个 post
在我的数据库中,我有一个 table "Students",它有一个属性 "id" 作为主键。 我的 AppController 如下所示:
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Auth', ['authenticate' =>
['Form' =>
['fields' =>
['username' => 'email',
'password' => 'password']
]
], 'loginAction' => [
'controller' => 'Students',
'action' => 'login'
]
]);
$this->Auth->config('authenticate', ['Ldap']);
}
public function isAuthorized()
{
if($this->Auth->user('departmentnumber') == "***") { return true; }
}
LdapAuthenticate class 就像我上面提到的 post 中的一样:
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Network\Request;
use Cake\Network\Response;
class LdapAuthenticate extends BaseAuthenticate {
protected $_host = '***' ;
public function authenticate(Request $request, Response $response) {
$username = $request->data['username'] ;
$password = $request->data['password'] ;
$ds = @ldap_connect($this->_host) ;
if (!$ds) {
throw \Cake\Error\FatalErrorException ('Unable to connect to LDAP host.') ;
}
$basedn = "cn=Users,dc=***";
$dn = "cn=$username, " . $basedn;
$ldapbind = @ldap_bind($ds, $dn, $password);
if (!$ldapbind) {
return false ;
}
$entry = ldap_first_entry ($ldapbind) ;
$attrs = ldap_get_attributes ($ldapbind, $entry) ;
$user = [] ;
// Loop
for ($i = 0 ; $i < $attrs["count"] ; $i++) {
$user[$attrs[$i]] = ldap_values ($ldapbind, $entry, $attrs[$i])[0] ;
}
// Then close it and return the authenticated user
ldap_unbind ($ldapbind) ;
return $user ;
}
}
在 StudentsController 中,我已经像 "bookmarker" 教程中那样实现了登录和注销功能:
public function login(){
if ($this->request->is('post')){
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
// user is not identified
$this->Flash->error('Your username or password is not correct');
}
}
public function logout(){
$this->Flash->success('You are now logged out');
return $this->redirect($this->Auth->logout());
}
当我打开任何页面时,我都成功地登陆了我的 login.ctp 页面。在我输入我的凭据并单击 "login" 后,我收到一个错误 "SQLSTATE[42S02]: Base table or view not found: 1146 Table '***_db.users' doesn't exist"。 所以我想我做错了什么,但没有足够的理解来找到哪里——不知道为什么它试图在我的数据库中找到一个不存在的 "users" table。
感谢大家提前帮我出出主意!
由于配置不正确。不要在您的身份验证配置中使用 Form
,而是使用 Ldap
:
$this->loadComponent('Auth', ['authenticate' =>
['Ldap' =>
['fields' =>
['username' => 'email',
'password' => 'password']
]
], 'loginAction' => [
'controller' => 'Students',
'action' => 'login'
]
]);