2015-09-21 59 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