Kez*_*Kez 5 php authentication cakephp
我有一个在 IIS 上运行的 Intranet 应用程序,使用 CakePHP 3。我可以从 IIS 访问服务器$_SERVER['AUTH_USER']变量,并且我想使用这个变量来验证用户的身份。
我在我的数据库中创建了一个用户表,其中包含一个我想要匹配的用户名字段AUTH_USER。我创建了一个自定义 Auth 组件,如下所示:
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\ORM\TableRegistry;
class AuthuserAuthenticate extends BaseAuthenticate
{
public function authenticate(Request $request, Response $response) {
$username = str_replace('DOMAIN\\', '', $_SERVER['AUTH_USER']);
$users = TableRegistry::get('Users');
$user = $users->find()->where(['username' => $username])->first();
if ($user) {
return $user;
} else {
$user = $this->Users->newEntity();
$user->username = $username;
if ($this->Users->save($user)) {
return $user;
} else {
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 AppController 中,initialize()我尝试使用自定义组件加载 Auth。
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username'],
'userModel' => 'Users'
],
],
'loginAction' => [
'controller' => 'Pages',
'action' => 'display'
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
$this->Auth->config('authenticate', 'Authuser');
Run Code Online (Sandbox Code Playgroud)
在这一点上,无论我尝试进入哪个页面,我都会被重定向,我不确定它是否无法通过身份验证或其他问题。
我曾尝试将此添加到 AppController 作为测试:
public function isAuthorized($user)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是我无法访问带有此代码的任何页面。谁能让我知道我做错了什么?
谢谢,
凯兹
您的身份验证组件未实现该authorize方法。
public function authorize($user, Request $request) {
// return true if authorized
// return false if not authorized
}
Run Code Online (Sandbox Code Playgroud)
其次,isAuthorized在使用组件时调用ControllerAuthorize。如果你想使用控制器身份验证,你应该使用ControllerAuthorizeinsted。
$this->loadComponent('Auth', [
'authenticate' => 'Controller'
]);
Run Code Online (Sandbox Code Playgroud)
另外:您正在配置BasicAuthenticate组件,然后立即覆盖配置。
| 归档时间: |
|
| 查看次数: |
1363 次 |
| 最近记录: |