php - 重写具有多个参数的复杂类

标签 php symfony

我正在尝试重写类UsernamePasswordFormAuthenticationListener

parameters:
    security.authentication.listener.form.class: AppBundle\Listener\LoginFormListener

class LoginFormListener extends UsernamePasswordFormAuthenticationListener
{
    /**
    * {@inheritdoc}
    */
    public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, CsrfProviderInterface $csrfProvider = null)
    {
        parent::__construct($securityContext, $authenticationManager, $sessionStrategy, $httpUtils, $providerKey, $successHandler, $failureHandler, $options, $logger, $dispatcher, $csrfProvider);
    }

    protected function attemptAuthentication(Request $request)
    {

        if (null !== $this->csrfTokenManager) {
            $csrfToken = $request->get($this->options['csrf_parameter'], null, true);

            if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
                throw new InvalidCsrfTokenException('Invalid CSRF token.');
            }
        }

        if ($this->options['post_only']) {
            $username = trim($request->request->get($this->options['username_parameter'], null, true));
            $password = $request->request->get($this->options['password_parameter'], null, true);
        } else {
            $username = trim($request->get($this->options['username_parameter'], null, true));
            $password = $request->get($this->options['password_parameter'], null, true);
        }

        $request->getSession()->set(Security::LAST_USERNAME, $username);

        $apiRequest = new ApiRequest();
        $apiRequest->addMethod('login', array('email' => $username, 'password' => $password));
        $response = $apiRequest->sendRequest();
        dump($response);
        exit;
    }
}

但是当我执行它时,我遇到了这个错误:

Catchable Fatal Error: Argument 1 passed to AppBundle\Listener\LoginFormListener::__construct() must implement interface Symfony\Component\Security\Core\SecurityContextInterface, instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage given, called in /Users/dimitri/Sites/rennes/app/cache/dev/appDevDebugProjectContainer.php on line 4039 and defined

知道如何才能完成这项工作吗?

最佳答案

您只需在类中将 SecurityContextInterface 类型提示更改为 TokenStorageInterface ,一切都应该可以正常工作 - 此类服务最近已更改(在 2.7 中已弃用),因此您的示例代码可能已过时。

检查blog post entry获取更多信息 SecurityContextInterfaceTokenStorageInterface

关于php - 重写具有多个参数的复杂类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689630/

相关文章:

php - 交响乐 2 : How to get route defaults by route name?

php - 在不更改 url 的情况下加载页面

php - 响应正常,但 $.ajax 上的成功方法未触发?

symfony - createFormBuilder()在哪里?

validation - 如何定义至少需要 2 个字段之一的 Symfony2 表单(无实体)?

php - DBAL Querybuilder 不更新

symfony - 向 LoadDataFixture 命令添加新选项

php - POST 请求始终为空 - Laravel 8

PHP XPATH 评估

php - 如何使用PHP向数据库中的2个表插入数据?