How to register an authentication handler service in Symfony2?

I need to write a basic authentication handler. In mine onAuthenticationFailure

, just for testing, I am var-dumping $request

. it should work with bad credentials, but nothing happened.

I think there is something wrong in my src\Acme\TestBundle\resources\Config\services.yml

:

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

      

This is a test class, the use statement has been removed for readability:

namespace Acme\TestBundle\Handler;

class AuthenticationHandler implements AuthenticationSuccessHandlerInterface,
    AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface
{

    function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $user = $token->getUser();
    }

    function onAuthenticationFailure(Request $request,
        AuthenticationException $exception)
    {
        var_dump($request);
        die();
    }

    public function onLogoutSuccess(Request $request)
    {
    }

}

      

+3


source to share


1 answer


You need to install the handler in the file security.yml

:



form_login:
    success_handler: authentication_handler
    failure_handler: authentication_handler
logout:
    success_handler: authentication_handler

      

+2


source







All Articles