2017-01-09 50 views
0

如果用户名不在数据库中,我已经扩展了DefaultAuthenticationFailureHandler以将用户重定向到注册页面。它适用于第一部分。 如果数据库中的用户名存在于我希望从Controller获取的默认行为中,即使用登录错误消息重定向到登录页面。为什么它不发布重定向?身份验证失败处理程序未返回响应

namespace UserBundle\Redirection; 

use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\RouterInterface; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; 
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler; 
use UserBundle\Controller\SecurityController; 

class AfterLoginFailureRedirection extends DefaultAuthenticationFailureHandler 
{ 
    /** 
    * @var \Symfony\Component\Routing\RouterInterface 
    */ 
    private $router; 
    /** 
    * @var \Symfony\Component\DependencyInjection\ContainerInterface 
    */ 
    private $container; 



    public function setRouter(RouterInterface $router) 
    { 
     $this->router = $router; 
    } 

    public function setContainer(ContainerInterface $container) 
    { 
     $this->container=$container; 

    } 


    /** 
    * @param Request $request 
    * @param AuthenticationException $token 
    * @return RedirectResponse 
    */ 
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
    { 


     //$request = $this->container->get('request'); 
     $token=$exception->getToken(); 
     $username=$token->getUsername(); 
     //$username = $request->request->get('email'); 
     $user = $this->container->get('fos_user.user_manager')->findUserByUsername($username); 

     if(!$user) { 
      $url=$this->container->get('router')->generate('fos_user_registration_register'); 
      return new RedirectResponse($url); 
     } 
     else 
     { 
      parent::onAuthenticationFailure($request,$exception); 
     } 
}} 

回答

1

你不会在你的其他情况下,返回任何

正确的代码应该是:

else { 
    return parent::onAuthenticationFailure($request,$exception); 
}