如何在Symfony 2中的login_check之后禁用重定向

use*_*963 31 php symfony

我需要在登录检查后禁用重定向,因为我只需要获取登录成功与否.提交/ login_check后url给我正确的数据,但保持重定向到/ login(失败时).
之后/ login是空白的.
我正在尝试使用extjs 4设置登录表单,所以我需要通过ajax post请求来验证.login_check应该进行身份验证,创建用户会话并返回它是成功还是失败,但不在任何地方转发.

我的login.html.twig看起来像:

{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
    { success:true }
{% else %}
    { success: false }
{% endif %}
Run Code Online (Sandbox Code Playgroud)

并在security.yml:

firewalls:
    main:
        form_login:
            provider: fos_userbundle
            failure_path:  null
            failure_forward: false
Run Code Online (Sandbox Code Playgroud)

Eln*_*mov 63

创建身份验证处理程序

namespace YourVendor\UserBundle\Handler;

// "use" statements here

class AuthenticationHandler
implements AuthenticationSuccessHandlerInterface,
           AuthenticationFailureHandlerInterface
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if ($request->isXmlHttpRequest()) {
            $result = array('success' => true);
            return new Response(json_encode($result));
        } else {
            // Handle non XmlHttp request here
        }
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        if ($request->isXmlHttpRequest()) {
            $result = array('success' => false);
            return new Response(json_encode($result));
        } else {
            // Handle non XmlHttp request here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将处理程序注册为服务:

services:
    authentication_handler:
        class: YourVendor\UserBundle\Handler\AuthenticationHandler
Run Code Online (Sandbox Code Playgroud)

在防火墙中注册服务:

firewalls:
    main:
        form_login:
            success_handler: authentication_handler
            failure_handler: authentication_handler
Run Code Online (Sandbox Code Playgroud)

这是一个粗略的例子,为您提供一般性的想法 - 您需要自己弄清楚细节.如果您遇到困难并需要进一步澄清,请将您的问题放在评论中,我将尝试详细说明该示例.