我的重定向适用于注销,但不适用于登录,它保留在登录页面但是引用程序具有适当的值(我在登录表单中显示{{ app.request.headers.get('referer') }}
)。Symfony2:登录成功后重定向到referer(FOSUserBundle)
Security.yml:
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager
use_referer: true
success_handler: authentication_handler
logout:
success_handler: authentication_handler
anonymous: true
处理器:
namespace LeJardinEbene\Bundle\Form\Handler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, LogoutSuccessHandlerInterface
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
public function onLogoutSuccess(Request $request)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
}
Services.yml:
services:
authentication_handler:
class: LeJardinEbene\Bundle\Form\Handler\AuthenticationHandler
是否有人KN这个问题是什么?
UPDATE
正如你劝我我分裂处理程序在2:
class LoginAuthenticationHandler
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
//$referer = $request->headers->get('referer');
$referer = $event->getRequest()->headers->get('referer');
return new RedirectResponse($referer);
}
}
class LogoutAuthenticationHandler implements LogoutSuccessHandlerInterface
{
public function onLogoutSuccess(Request $request)
{
$referer = $request->headers->get('referer');
return new RedirectResponse($referer);
}
}
services:
login_authentication_handler:
class: LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
logout_authentication_handler:
class: LeJardinEbene\Bundle\Form\Handler\LogoutAuthenticationHandler
但现在我得到以下错误:
Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Http\Authentication\CustomAuthenticationSuccessHandler::__construct() must implement interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface, instance of LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler given
更新2
namespace LeJardinEbene\Bundle\Form\Handler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
class LoginAuthenticationHandler extends AuthenticationSuccessHandlerInterface
{
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$referer = $event->getRequest()->headers->get('referer');
return new RedirectResponse($referer);
}
}
产生错误:
Fatal error: Class LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler cannot extend from interface Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface in /Applications/XAMPP/xamppfiles/htdocs/symfony2/src/LeJardinEbene/Bundle/Form/Handler/LoginAuthenticationHandler.php on line 22
更新3
我已经尝试过,它会导致这个错误:
Fatal error: Class LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface::onAuthenticationSuccess) in /Applications/XAMPP/xamppfiles/htdocs/symfony2/src/LeJardinEbene/Bundle/Form/Handler/LoginAuthenticationHandler.php on line 22
,如果我这样做一切都改变了没有?我的意思是,没有更多的$事件等,但$请求和$令牌再次作为参数...这不起作用,当我打开这个话题,arghhhhhhhhhhh!
UPDATE 4
services.yml
services:
login_handler:
class: LeJardinEbene\Bundle\Form\Handler\LoginAuthenticationHandler
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }
security.yml
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: security.csrf.token_manager
use_referer: true
success_handler: login_handler
logout:
success_handler: logout_authentication_handler
anonymous: true
没有错误了,但不回去引用者...也不会是因为我的security.yml的?也许它不是“success_handler:login_authentication_handler”。看起来令人难以置信,它仍然无法正常工作。无论如何非常感谢您的帮助 –
Waw ...肯定是一个错误,尝试在'services.yml'中和在'success_handler'的安全性中更改服务的名称。 对不起! – chalasr
尝试但相同,请参阅更新4 ;-) –