dongliao1949 2016-01-25 10:00
浏览 70
已采纳

如何使用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.

The firewall looks like this :

$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) {
    ...
});

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)
    {
       ...
    }
}

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 ?

Thanks !

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-01-25 12:02
    关注

    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']
        );
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大