[英]Symfony 3: How to redirect from within InteractiveLoginEvent listener
在Symfony 3.4.0中,从一个侦听InteractiveLoginEvent
的事件侦听器中,我想重定向到另一个路由,因此我想响应一个重定向。
但是InteractiveLoginEvent
对象没有“ setResponse”方法。 那么,使用InteractiveLoginEvent
修改响应以便将重定向发送给用户的首选方法是什么?
要在成功登录后执行重定向,可以添加一个自定义成功处理程序,该处理程序应实现AuthenticationSuccessHandlerInterface 。
AuthenticationSuccessHandler.php
<?php
namespace AppBundle\Security\Authentication\Handler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
return new RedirectResponse($this->router->generate('homepage'));
}
}
services.yml
services:
AppBundle\Security\Authentication\Handler:
arguments: ['@router']
安全性
security:
# ...
firewalls:
form_login:
# ...
use_referer: false
success_handler: AppBundle\Security\Authentication\Handler\AuthenticationSuccessHandler
既然您在注释中提出要求,那么您不仅可以自己实现整个接口,还可以扩展默认的成功处理程序,覆盖成功方法,对其执行所需操作,然后调用父方法以允许symfony正常执行操作
单击下面的完整示例,了解如何覆盖处理程序https://www.codereviewvideos.com/course/symfony-3-for-beginners/video/bonus-how-to-show-a-flash-message-on-尝试成功登录或失败
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.