2016-02-12 60 views
1

我的重定向适用于注销,但不适用于登录,它保留在登录页面但是引用程序具有适当的值(我在登录表单中显示{{ 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 
+0

没有错误了,但不回去引用者...也不会是因为我的security.yml的?也许它不是“success_handler:login_authentication_handler”。看起来令人难以置信,它仍然无法正常工作。无论如何非常感谢您的帮助 –

+0

Waw ...肯定是一个错误,尝试在'services.yml'中和在'success_handler'的安全性中更改服务的名称。 对不起! – chalasr

+0

尝试但相同,请参阅更新4 ;-) –

回答

1

试图实现你的AuthenticationHandler下面的方法:

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) 
{ 
    $user = $event->getAuthenticationToken()->getUser(); 
} 

它解决了我的问题。

更新

如果添加此方法不解决这个问题对你来说,添加以下内容:

$referer = $event->getRequest()->headers->get('referer'); 

return new RedirectResponse($referer); 

之后,如果没有触发事件,您应该分开的两个AuthenticationHandler(例如LogoutSuccessHandler和AuthenticationHandler)声明两个服务并指定您正在侦听的事件以及应调用哪个方法。

例子:

services: 
    authentication_handler: 
     class: LeJardinEbene\Bundle\Form\Handler\AuthenticationHandler 
     tags: 
      - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin } 

UPDATE2

Here a gist包括您LoginAuthenticationHandler和相应的服务声明。

+0

好吧,但你用$用户做什么呢? –

+0

查看我的更新。只要像下面那样实施它,就可以为我正确解决原始事件。但你也可以做你的重定向。 – chalasr

+0

谢谢你;请看我的更新 –

0

尝试在2个处理程序中分隔处理程序LeJardinEbene \ Bundle \ Form \ Handler \ AuthenticationHandler,其中一个实现AuthenticationSuccessHandlerInterface和其他实现LogoutSuccessHandlerInterface。该代码将是这样的:

<?php 

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 
{ 
public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
{ 
    $referer = $request->headers->get('referer'); 
    return new RedirectResponse($referer); 
} 
} 

<?php 

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 LogoutAuthenticationHandler implements LogoutSuccessHandlerInterface 
{ 

public function onLogoutSuccess(Request $request) 
{ 
    $referer = $request->headers->get('referer'); 
    return new RedirectResponse($referer); 
} 
} 

对不起我的英语

+0

谢谢你,但它并没有改变任何东西,虽然我也定义了2个服务,一个用于登录,另一个用于注销。并更新security.ml也;请参阅我的更新 –

+0

像@chalasr评论之前使您的LoginAuthenticationSuccessHandler扩展Symfony \ Component \ Security \ Http \ Authentication \ AuthenticationSuccessHandlerInte rface – Guillermo

+0

不工作...(更新2我的文章) –