2
我想覆盖类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
任何想法如何,我可以使这项工作?
根据您要覆盖类的定义,它接受TokenStorageInterface'的'的实例,但在你的类,第一个参数是一个实例'SecurityContextInterface'。查看原始代码[此处](https://github.com/symfony/security-http/blob/master/Firewall/UsernamePasswordFormAuthenticationListener.php)并相应地修改您的课程。 – Artamiel
你说得对,我不知道我怎么可能错过它?但我只是改变了它,它也做了同样的错误。我已经清除了缓存......编辑:我只是忘了使用这个类,现在工作正常。 –