2016-02-12 90 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我的文章) –