gpt4 book ai didi

php - 如何使用 Silex 框架实现自定义身份验证成功处理程序?

转载 作者:可可西里 更新时间:2023-11-01 00:58:16 35 4
gpt4 key购买 nike

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

防火墙看起来像这样:

$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"];
}),
),
),
));

我发现我必须注册这样的服务:

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

我还创建了一个实现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、连接的日期和时间,如果他登录失败或成功) , ETC。)。每当用户尝试登录时,我如何设法调用此函数?

谢谢!

最佳答案

添加认证成功处理器

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

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

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

并在应用程序中注册

$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']
);
});

关于php - 如何使用 Silex 框架实现自定义身份验证成功处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34989670/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com