Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

redirect - How to go back to referer after login failure?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 AcmeMainBundleHandler;

use SymfonyComponentSecurityHttpLogoutLogoutSuccessHandlerInterface;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationFailureHandlerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationRedirectResponse;

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: AcmeMainBundleHandlerAuthenticationHandler

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...