登录失败后如何回到referer?

时间:2012-01-29 23:27:13

标签: redirect symfony login referer

对于登录成功,有一个参数use_referer: true。对于登录失败,只有failure_path,这不是我正在寻找的。

第二件事:怎么做并传递错误信息?

第三件事:如何在退出后返回推荐人?

1 个答案:

答案 0 :(得分:52)

我解决了。

有解决方案:How to disable redirection after login_check in Symfony 2

以下是解决我问题的代码:

<?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例如:

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 

和config.yml:

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