如何避免 FOSUserBundle 在错误时重定向到 /login 凭据错误


How to avoid that FOSUserBundle redirect to /login when error bad credential

在我的应用程序中,登录框被集成到我的当前页面中。除非用户在其用户名/密码关联中出错,否则它可以正常工作。

然后呈现登录页面,并且此页面未集成到我的网站中(而我希望将带有错误消息(如"凭据错误"之类的登录框放入布局中)。

如何才能在当前页面上传递错误的凭据?我不想被重定向到www.my网站/登录,而是重定向到用户可以登录的收件箱的当前页面。

在我的代码中,我只覆盖了"login.html.twig",这样它就不会扩展任何布局,并且可以简单地集成到我的当前页面(Layout.html.twig)中:

  {{ render(controller("FOSUserBundle:Security:login"))   }} 

请在app/config下检查您的security.yml文件。

查找此代码:

form_login:
    provider:               fos_userbundle
    login_path:             fos_user_security_login

并根据您的需要更改login_path,例如:

login_path:    /

您可以使用 ajax 进行身份验证或侦听登录失败事件

服务.yml

login_failure_handler:
    class:  'App'UserBundle'Handler'LoginFailureHandler'
    arguments:  [ '@router', '@security.context' ]
    tags:
        - { name: 'monolog.logger', channel: 'security' }

登录失败处理程序.php

class LoginFailureHandler implements AuthenticationFailureHandlerInterface
{
    /** @var 'Symfony'Component'Routing'Router */
    protected $router;
    /** @var 'Symfony'Component'Security'Core'SecurityContext */
    protected $security;
    /**
     * @param Router $router
     * @param SecurityContext $security
     */
    public function __construct(Router $router, SecurityContext $security) {
        $this->router = $router;
        $this->security = $security;
    }
    /**
     * This is called when an interactive authentication attempt fails. This is
     * called by authentication listeners inheriting from
     * AbstractAuthenticationListener.
     *
     * @param Request $request
     * @param AuthenticationException $exception
     *
     * @return Response The response to return, never null
     */
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        // if AJAX login
        if ( $request->isXmlHttpRequest() ) {
            $array = array( 'success' => false, 'message' => $exception->getMessage() ); // data to return via JSON
            $response = new Response( json_encode( $array ) );
            $response->headers->set( 'Content-Type', 'application/json' );
            return $response;
            // if form login
        } else {
            // set authentication exception to session
            $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
            return new RedirectResponse( $this->router->generate( 'your_route' ) );
        }
    }

}

您可以捕获错误并显示您的视图:

控制器示例:

public function loginAction()
    {
        $request = $this->getRequest();
        $session = $request->getSession();
        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }
        return $this->render('your_bundle:login.html.twig', array(
            // last username entered by the user
            'last_username' => $session->get(SecurityContext::LAST_USERNAME),
            'error'         => $error,
        ));
    }

然后在您的视图中显示它:

{{ error.message }

如果你的意思是在FOSUserBundle中修改login.html.twig,你必须添加这个:

{% block fos_user_content %}
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Security/login.html.twig#L7