2015-09-21 31 views
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 

任何想法如何,我可以使这项工作?

+1

根据您要覆盖类的定义,它接受TokenStorageInterface'的'的实例,但在你的类,第一个参数是一个实例'SecurityContextInterface'。查看原始代码[此处](https://github.com/symfony/security-http/blob/master/Firewall/UsernamePasswordFormAuthenticationListener.php)并相应地修改您的课程。 – Artamiel

+0

你说得对,我不知道我怎么可能错过它?但我只是改变了它,它也做了同样的错误。我已经清除了缓存......编辑:我只是忘了使用这个类,现在工作正常。 –

回答

2

您可以简单地在您的类中更改类型提示为TokenStorageInterface,并且所有应该都可以正常工作 - 此类服务最近已更改(2.7中不推荐使用),因此您的示例代码可能已过时。

检查blog post entry以获得更多信息关于 SecurityContextInterface VS TokenStorageInterface