身份验证失败处理程序未返回响应 [英] Authentication Failure Handler did not return a Response

查看:24
本文介绍了身份验证失败处理程序未返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了 DefaultAuthenticationFailureHandler 以便在用户名不在数据库中时将用户重定向到注册页面.它适用于第一部分.如果用户名存在于数据库中,我希望控制器采用默认行为,即重定向到带有登录错误消息的登录页面.为什么它不发出重定向?

I have extended the DefaultAuthenticationFailureHandler in order to redirect users to register page if the username is not in the database. it works fine for the first part. If the username exists in the database I want from the Controller the default behavior i.e. to redirect to the login page with the login error message. Why it is not issuing a redirect?

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);
        }
}}

推荐答案

在 else 情况下您不会返回任何内容

You don't return anything in your else case

正确的代码应该是:

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

这篇关于身份验证失败处理程序未返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆