2012-01-29 5 views
21

로그인 성공을 위해 매개 변수 use_referer: true가 있습니다. 로그인에 실패한 경우에만 failure_path이 있는데, 이는 내가 찾고있는 것이 아닙니다.로그인 실패 후 referer로 돌아가는 방법은 무엇입니까?

두 번째 사항 :이를 수행하고 오류 메시지를 전달하는 방법은 무엇입니까?

세 번째 사항 : 로그 아웃 후 참조로 돌아가는 방법은 무엇입니까?

+0

왜 실패 경로가 로그인 프롬프트가되지 않습니까? – JamesHalsall

+1

모든 서브 페이지에서 로그인 할 수 있기 때문입니다. 지정된 URL에 하나의 로그인 양식이 없으므로 다른 사용자 프로필 페이지 등에서 로그인 할 때 홈페이지로 리디렉션하고 싶지 않습니다. –

답변

49

해결했습니다.

이 솔루션입니다 : How to disable redirection after login_check in Symfony 2

여기 내 문제 해결 코드 :

form_login: 
    check_path: /access/login_check 
    login_path:/
    use_referer: true 
    failure_handler: authentication_handler 
logout: 
    path: /access/logout 
    target:/
    success_handler: authentication_handler 

및 구성하기 :

<?php 

namespace Acme\MainBundle\Handler; 

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface; 
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface 
{ 
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
    {  
     $referer = $request->headers->get('referer');  
     $request->getSession()->setFlash('error', $exception->getMessage()); 

     return new RedirectResponse($referer); 
    } 

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

이벤트가 예를 들어 security.yml에 추가 처리하기를 .yml :

services: 
    authentication_handler: 
     class: Acme\MainBundle\Handler\AuthenticationHandler 
+4

$ request-> headers-> get ('referer'); 때때로 null을 반환합니다 (Firefox에서 재현했습니다). 이 솔루션은 신뢰할 수 없으며 대체 또는 다른 솔루션이 필요합니다. ''$ request-> getSession() -> setFlash ('error', $ exception-> getMessage());''' '''FatalErrorException : Error : – Julien

+0

@ wojciech-kulik 다음 오류가 발생했습니다 : 정의되지 않은 메소드를 호출합니다. /var/www/symfony/src/Application/Sonata/UserBundle/Form/Handler/AuthenticationHandler.php 라인에서 Symfony \ Component \ HttpFoundation \ Session \ Session :: setFlash()를 호출하십시오.''' – jcarlosweb

+0

@webyseo 아마 다른 버전의 Symphony가 있고 플래시 메시지가 다른 방식으로 설정되어 있습니다. 아마도 도움이 될 것입니다 : http://stackoverflow.com/questions/13348534/symfony-2-setting-a-flash-message-outside-of-controller –

관련 문제