没有标识的cakephp授权错误

没有标识的cakephp授权错误,cakephp,Cakephp,我使用带有授权2插件的CakePHP4。 我有一个检查用户是否具有“管理员”角色的策略 当在应用程序上标识用户=>设置了标识时,它可以正常工作 但是当用户未被识别时=>identity为空 对策略的调用返回错误: Argument 1 passed to App\Policy\UserPolicy::canAdminAccess() must be an instance of Authorization\IdentityInterface, null given 策略函数:($user未标识

我使用带有授权2插件的CakePHP4。 我有一个检查用户是否具有“管理员”角色的策略

当在应用程序上标识用户=>设置了标识时,它可以正常工作

但是当用户未被识别时=>identity为空

对策略的调用返回错误:

Argument 1 passed to App\Policy\UserPolicy::canAdminAccess() must be an instance of Authorization\IdentityInterface, null given
策略函数:($user未标识时为空)

以及呼叫控制器:

public function beforeFilter(EventInterface $event)
    {
        parent::beforeFilter($event);
        $this->Authorization->authorize($this->user,'adminAccess');
    }
你知道怎么解决这个问题吗

谢谢

传递给App\Policy\UserPolicy::canAdminAccess()的参数1必须为 授权\IdentityInterface的实例,给定null

您在何处以及如何定义
$user
属性

public function beforeFilter(EventInterface $event)
    {
        parent::beforeFilter($event);
        debug($this->user); // what is output ??
        //$this->Authorization->authorize($this->user,'adminAccess');

        // to fix, try
        $user = $this->Authentication->getIdentity()->getOriginalData();
        $this->Authorization->authorize($user,'adminAccess');
    }

授权取决于身份验证,当用户未经身份验证时,通常没有必要让他们继续进行授权检查

我建议您考虑从默认的<代码>控制器中更改认证组件的代码>标识>事件/代码>选项。启动< <代码>:(发生在<代码>控制器::预过滤()/代码>)到<代码>控制器。初始化(这就是调用<代码>控制器::

$this->loadComponent('Authentication.Authentication'[
'identityCheckEvent'=>'控制器。初始化',
]);
这将检查组件的
beforeFilter()
回调中的标识,该回调在控制器的
beforeFilter()回调之前被调用

或者,您可以在
beforeFilter()方法中自己检查身份:

/。。。
如果(!$this->Authentication->getIdentity()){
抛出新的未经身份验证的异常(
$this->Authentication->getConfig('unauthenticatedMessage','')
);
}
// ...
$this->Authorization->authorize($this->user,'adminAccess');
请注意,对于应该允许在没有身份验证的情况下访问的操作,您需要确保既不应用身份验证检查,也不应用授权检查!比如:

$unauthenticatedAllowed=在数组中(
$this->request->getParam('action'),
$this->Authentication->getUnauthenticatedActions(),
真的
);
// ...
如果(!$unauthenticatedAllowed){
如果(!$this->Authentication->getIdentity()){
抛出新的未经身份验证的异常(
$this->Authentication->getConfig('unauthenticatedMessage','')
);
}
// ...
$this->Authorization->authorize($this->user,'adminAccess');
}
此时,您可能还想问问自己,将公共端点与受保护端点分开是否有意义,例如将它们放在单独的控制器和/或前缀中,以便您可以仅在受保护端点上应用身份验证/授权中间件,并在没有任何身份验证/授权检查的情况下保留公共的

另见


为什么您的应用程序一开始就允许未经身份验证的用户访问那么远?@ndm,我真的不理解您的观点,applaction怎么能禁止访问?每个人都可以调用url(/admin/mycontroller/myaction)。控件正是对$this->Authorization->authorize($this->user,'adminAccess')的调用;我的观点是,未经身份验证的用户甚至不应该进入检查授权的阶段,后者自然取决于前者。$user在AppController beforeFilter中设置:默认情况下,如果($this->Authentication->getIdentity()){$identity=$this->Authentication->getIdentity()$this->user=$identity->getOriginalData();…这是一个很好的跟踪…当未经过身份验证时,此->身份验证->getIdentity()返回null,以便在未验证身份验证时它不起作用,然后将请求重定向到登录页应用程序的某些部分在未验证的情况下对所有人开放。因此,在AppController中,如果未验证,我无法重定向到登录页…我已经使用前缀进行管理操作。我认为在检查之前检查身份验证ng授权是解决方案。
public function beforeFilter(EventInterface $event)
    {
        parent::beforeFilter($event);
        debug($this->user); // what is output ??
        //$this->Authorization->authorize($this->user,'adminAccess');

        // to fix, try
        $user = $this->Authentication->getIdentity()->getOriginalData();
        $this->Authorization->authorize($user,'adminAccess');
    }