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

时间:2022-10-20 10:51:26

I need to disable redirection after login check, because I need to get only that the login was success or not. After submission /login_check url give me the right data, but keep redirecting to /login (on failure).
/login is blank after that.
I am trying to set up login form using extjs 4 so I need to validate trough an ajax post request. login_check should authenticate, create user session and return whether it was success or failure, but no forwarding anywhere.

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

my login.html.twig looks like:

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

{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
    { success:true }
{% else %}
    { success: false }
{% endif %}

and in security.yml:

并在security.yml中:

firewalls:
    main:
        form_login:
            provider: fos_userbundle
            failure_path:  null
            failure_forward: false

2 个解决方案

#1


63  

Create an authentication handler:

创建身份验证处理程序

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

Register the handler as a service:

将处理程序注册为服务:

services:
    authentication_handler:
        class: YourVendor\UserBundle\Handler\AuthenticationHandler

Register the service in the firewall:

在防火墙中注册服务:

firewalls:
    main:
        form_login:
            success_handler: authentication_handler
            failure_handler: authentication_handler

This is a rough example to give you the general idea — you'll need to figure out the details by yourself. If you're stuck and need further clarifications, put your questions in the comments and I'll try to elaborate the example.

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

#2


1  

The normal symfony flow is to redirect you to a login page if you are not logged in, which works fine for humans. But you seems to be looking for a programmatic solution.

正常的symfony流程是在您未登录时将您重定向到登录页面,这对人类来说很好。但您似乎在寻找程序化解决方案。

Have you tried setting _target_path in your form, to declare what the "next page" should be? Symfony is always going to forward you somewhere, but you can set that somewhere to wherever you want.

您是否尝试在表单中设置_target_path,以声明“下一页”应该是什么? Symfony总是会把你转发到某个地方,但是你可以将它设置在任何你想要的地方。

I found these two pages useful for describing the inner workings of the login form:

我发现这两个页面对于描述登录表单的内部工作方式很有用:

#1


63  

Create an authentication handler:

创建身份验证处理程序

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

Register the handler as a service:

将处理程序注册为服务:

services:
    authentication_handler:
        class: YourVendor\UserBundle\Handler\AuthenticationHandler

Register the service in the firewall:

在防火墙中注册服务:

firewalls:
    main:
        form_login:
            success_handler: authentication_handler
            failure_handler: authentication_handler

This is a rough example to give you the general idea — you'll need to figure out the details by yourself. If you're stuck and need further clarifications, put your questions in the comments and I'll try to elaborate the example.

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

#2


1  

The normal symfony flow is to redirect you to a login page if you are not logged in, which works fine for humans. But you seems to be looking for a programmatic solution.

正常的symfony流程是在您未登录时将您重定向到登录页面,这对人类来说很好。但您似乎在寻找程序化解决方案。

Have you tried setting _target_path in your form, to declare what the "next page" should be? Symfony is always going to forward you somewhere, but you can set that somewhere to wherever you want.

您是否尝试在表单中设置_target_path,以声明“下一页”应该是什么? Symfony总是会把你转发到某个地方,但是你可以将它设置在任何你想要的地方。

I found these two pages useful for describing the inner workings of the login form:

我发现这两个页面对于描述登录表单的内部工作方式很有用: