Symfony 3 - LoginAction 上的监听器 - kernelException [英] Symfony 3 - listener on LoginAction - kernelException
问题描述
我想在我的 LoginAction 上使用监听器,但我正在使用的监听器不起作用.
I would like to use a Listener on my LoginAction but the one I am using, isn't working.
我有一个实体:
class User implements AdvancedUserInterface {
public function isEnabled(){}
...
}
}
所以,当我尝试连接我的用户时,我收到了好消息:
So, when, I try to connect my user i have the good message :
用户被禁用
在此操作中,我使用了这样的 ExceptionListener :
On this action, I use an ExceptionListener like this :
# src/AppBundle/EventListener/ExceptionListener.php
namespace AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\DisabledException;
class ExceptionListener
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
die('-------');
$ex = $event->getException();
if($ex instanceof DisabledException) {
$url = $this->router->generate('accounts.please_activate');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
}
我的监听器工作,因为,如果我在构造中做一个 die
,没关系,但是,我的 die
在函数 onKernelException 中不起作用
My listener work, cause, if i do a die
in the construct, it's ok, but, my die
doesn't work in the function onKernelException
所以我重定向到特定路线不起作用...
So my redirect to the specific route doesn't work...
services.yml
services:
app.exception_listener:
class: AppBundle\EventListener\ExceptionListener
arguments: ['@router']
tags:
- { name: kernel.event_listener, event: kernel.exception }
问题:
为什么我无法将被视为禁用"的用户重定向到特定路由?
Question:
Why am I unable to redirect a user that is considered "disabled" to a specific route?
再说明一下,这被认为是好的做法吗?
On a second note, is this considered good practice?
推荐答案
您应该使用自定义登录处理程序来处理登录失败.
You should handle the login failures with customized login handlers.
创建一个实现Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface
的新服务:
Creare a new service that implements Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface
:
namespace AppBundle\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\DisabledException;
class FailLoginHandler implements AuthenticationFailureHandlerInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if($exception instanceof DisabledException) {
$url = $this->router->generate('accounts.please_activate');
return new RedirectResponse($url);
}
}
}
定义一个新服务
services:
app.login_fail:
class: AppBundle\Security\FailLoginHandler
arguments: ['@router']
然后在 security.yml 上的防火墙配置下添加 failure_handler
选项:
then add the failure_handler
option under the configuration of your firewall on security.yml:
security:
firewalls:
firewallname:
failure_handler: app.login_fail
这篇关于Symfony 3 - LoginAction 上的监听器 - kernelException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!