如果用户名不在数据库中,我已经扩展了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);
}
}}
答案 0 :(得分:1)
你不会在你的其他情况下退回任何内容
正确的代码应该是:
else {
return parent::onAuthenticationFailure($request,$exception);
}