cakephp 4 - 身份验证 2 - 未识别时如何显示消息
cakephp 4 - authentication 2 - How to display message when not identified
我在 cakephp4 中使用身份验证 (2) 插件。
我已经设置:
'unauthenticatedRedirect' => '/users/login',
为了重定向需要身份验证的请求。它工作正常。
但我想添加一条消息,例如一条快讯,内容为 "You must be log in to acceess this page"。
有简单的方法吗?
谢谢
尚无具体功能:https://github.com/cakephp/authentication/issues/316
它可以通过许多不同的方式解决,我个人之前通过在扩展身份验证组件中捕获 Authentication\Authenticator\UnauthenticatedException
,通过覆盖 \Authentication\Controller\Component\AuthenticationComponent::doIdentityCheck()
:
来解决这个问题
<?php
// src/Controller/Component/AppAuthenticationComponent.php
/*
load in `AppController::initialize()` via:
$this->loadComponent('Authentication', [
'className' => \App\Controller\Component\AppAuthenticationComponent::class,
]);
*/
namespace App\Controller\Component;
use Authentication\Authenticator\UnauthenticatedException;
use Authentication\Controller\Component\AuthenticationComponent;
use Cake\Controller\Component\FlashComponent;
/**
* @property FlashComponent $Flash
*/
class AppAuthenticationComponent extends AuthenticationComponent
{
public $components = [
'Flash'
];
protected function doIdentityCheck(): void
{
try {
parent::doIdentityCheck();
} catch (UnauthenticatedException $exception) {
$this->Flash->error(__('You must be logged in to access this page.'));
throw $exception;
}
}
}
您也可以在您的应用程序控制器中手动执行此操作,因为您必须禁用插件组件的自动身份检查,并自行执行该检查:
// src/Controller/AppController.php
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Authentication.Authentication', [
'requireIdentity' => false
]);
}
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);
$action = $this->request->getParam('action');
if (
!in_array($action, $this->Authentication->getUnauthenticatedActions(), true) &&
!$this->Authentication->getIdentity()
) {
$this->Flash->error(__('You must be logged in to access this page.'));
throw new UnauthenticatedException('No identity found.');
}
}
我在 cakephp4 中使用身份验证 (2) 插件。 我已经设置:
'unauthenticatedRedirect' => '/users/login',
为了重定向需要身份验证的请求。它工作正常。
但我想添加一条消息,例如一条快讯,内容为 "You must be log in to acceess this page"。
有简单的方法吗?
谢谢
尚无具体功能:https://github.com/cakephp/authentication/issues/316
它可以通过许多不同的方式解决,我个人之前通过在扩展身份验证组件中捕获 Authentication\Authenticator\UnauthenticatedException
,通过覆盖 \Authentication\Controller\Component\AuthenticationComponent::doIdentityCheck()
:
<?php
// src/Controller/Component/AppAuthenticationComponent.php
/*
load in `AppController::initialize()` via:
$this->loadComponent('Authentication', [
'className' => \App\Controller\Component\AppAuthenticationComponent::class,
]);
*/
namespace App\Controller\Component;
use Authentication\Authenticator\UnauthenticatedException;
use Authentication\Controller\Component\AuthenticationComponent;
use Cake\Controller\Component\FlashComponent;
/**
* @property FlashComponent $Flash
*/
class AppAuthenticationComponent extends AuthenticationComponent
{
public $components = [
'Flash'
];
protected function doIdentityCheck(): void
{
try {
parent::doIdentityCheck();
} catch (UnauthenticatedException $exception) {
$this->Flash->error(__('You must be logged in to access this page.'));
throw $exception;
}
}
}
您也可以在您的应用程序控制器中手动执行此操作,因为您必须禁用插件组件的自动身份检查,并自行执行该检查:
// src/Controller/AppController.php
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Authentication.Authentication', [
'requireIdentity' => false
]);
}
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);
$action = $this->request->getParam('action');
if (
!in_array($action, $this->Authentication->getUnauthenticatedActions(), true) &&
!$this->Authentication->getIdentity()
) {
$this->Flash->error(__('You must be logged in to access this page.'));
throw new UnauthenticatedException('No identity found.');
}
}