且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

登录失败后如何返回引荐来源网址?

更新时间:2023-02-16 19:19:55

我解决了.

有解决方案:如何在Symfony 2中的login_check之后禁用重定向

这是解决我的问题的代码:

and here is code which solves my problem:

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

to handle events add to security.yml for example:

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:

and to config.yml:

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