How to go back to referer after login failure?

浪子不回头ぞ 提交于 2019-11-27 00:59:18

问题


For login success there is a parameter use_referer: true. For login failure there is only failure_path, which isn't what I'm looking for.

Second thing: How to do that and pass error message?

Third thing: How to go back to referrer after logout?


回答1:


I solved it.

There is solution: How to disable redirection after login_check in Symfony 2

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);
    }
}

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 

and to config.yml:

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


来源:https://stackoverflow.com/questions/9057745/how-to-go-back-to-referer-after-login-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!