如何使用Silex框架实现自定义身份验证成功处理程序? [英] How to implement a custom authentication success handler with Silex framework?

查看:99
本文介绍了如何使用Silex框架实现自定义身份验证成功处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户登录(成功和失败)时跟踪一些数据,但是我真的不知道该怎么做.

I'd like to track some data when users are logging in (success and failure) but I don't really know how to do it.

防火墙看起来像这样:

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'secured' => array(
            'pattern' => '^/',
            'anonymous' => true,
            'logout' => true,
            'form' => array('login_path' => '/login',
                            'check_path' => '/login_check',
                            ),
            'users' => $app->share(function () use ($app) {
                return $app["dao.identifiant"];
            }),
        ),
    ),
));

我发现我必须注册一个服务,如:

I found that I have to register a service like :

$app['security.authentication.success_handler.secured'] = $app->share(function ($app) {
    ...
});

我还创建了一个实现 AuthenticationSuccessHandlerInterface 的自定义类:

And I also created a custom class implementing the AuthenticationSuccessHandlerInterface :

<?php

namespace myproject\Authentication;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
       ...
    }
}

我有一个名为 Connection 的类,其中有一个函数可以在数据库中记录一些信息(例如用户ID,连接日期和时间,如果他失败或成功登录的话)等等).每当用户尝试登录时,我如何设法调用此函数?

I have a class called Connection in which I have a function to record some information in my database (like the user ID, date & time of the connection, if he failed or succeded to log in, etc.). How can I manage to call this function whenever a user tries to log in ?

谢谢!

推荐答案

添加身份验证成功处理程序

Add authentication success handler

namespace Your\Namespace;

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as BaseDefaultAuthenticationSuccessHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class DefaultAuthenticationSuccessHandler extends BaseDefaultAuthenticationSuccessHandler
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        // your actions

        return parent::onAuthenticationSuccess($request, $token);
    }
}

添加身份验证失败处理程序

Add authentication failure handler

namespace Your\Namespace;

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler as BaseDefaultAuthenticationFailureHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class DefaultAuthenticationFailureHandler extends BaseDefaultAuthenticationFailureHandler
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        // your actions

        return parent::onAuthenticationFailure($request, $exception);
    }
}

并在应用程序中注册它们

and register them in application

$app['security.authentication.success_handler.secured'] = $app->share(function () use ($app) {
    $handler = new \Your\Namespace\DefaultAuthenticationSuccessHandler(
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form']
    );
    $handler->setProviderKey('secured');

    return $handler;
});

$app['security.authentication.failure_handler.secured'] = $app->share(function () use ($app) {
    return new \Your\Namespace\DefaultAuthenticationFailureHandler(
        $app,
        $app['security.http_utils'],
        $app['security.firewalls']['secured']['form'],
        $app['logger']
    );
});

这篇关于如何使用Silex框架实现自定义身份验证成功处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆