我正在尝试重写类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获取更多信息
SecurityContextInterface
与 TokenStorageInterface
关于php - 重写具有多个参数的复杂类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689630/