Jquery 如何修复symfony2的ajax对话框中的登录表单

Jquery 如何修复symfony2的ajax对话框中的登录表单,jquery,ajax,symfony,login,fosuserbundle,Jquery,Ajax,Symfony,Login,Fosuserbundle,我在symfony2中为我的登录表单实现了一个对话框,它工作得很好,只是我想用更多的逻辑来处理返回,现在我不知道怎么做,因为防火墙配置会选择登录提交 如果登录失败,我的对话框的html将替换为登录控制器返回的新html,这一切都很好。 但是如果成功登录,我的登录对话框的html将替换为我的整个站点(因为成功的symfony2登录将重定向到起始页…) 在平面PHP中,我会将其添加到登录控制器 if (login_successful) { return "success"; } 在我的对话框

我在symfony2中为我的登录表单实现了一个对话框,它工作得很好,只是我想用更多的逻辑来处理返回,现在我不知道怎么做,因为防火墙配置会选择登录提交

如果登录失败,我的对话框的html将替换为登录控制器返回的新html,这一切都很好。 但是如果成功登录,我的登录对话框的html将替换为我的整个站点(因为成功的symfony2登录将重定向到起始页…)

在平面PHP中,我会将其添加到登录控制器

if (login_successful) {
  return "success";
}
在我的对话框函数中

if (returned_data == "success") {
  refresh_page(); // or location.href('something')
}
else
{
  // replace dialog_html with the returned html
}
但是,当我查看处理FOS用户包中的登录表单提交的操作时,我发现了这一点

public function checkAction()
{
    throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}
所以我意识到这些都是在symfony2的幕后处理的,那么我能做到这一点吗

实际示例代码(JS)

而表格

<form action="{{ path("fos_user_security_check") }}" method="post" id="login-form">
    <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

    <TABLE>
        <TR>
            <TD><label for="username">Login</label></TD>
            <TD><input type="text" placeholder="användare" id="username" name="_username" value="{{ last_username }}" /></TD>
        </TR>
        <TR>
            <TD><label for="password">Lösenord</label></TD>
            <TD><input type="password" placeholder="lösenord" id="password" name="_password" /></TD>
        </TR>
        <TR>
            <TD><label for="remember_me">Kom ihåg mig</label></TD>
            <TD><input type="checkbox" id="remember_me" name="_remember_me" value="on" /></TD>
        </TR>
        <!--
        <TR>
            <TD></TD>
            <TD align="right"><input type="submit" id="_submit" name="_submit" value="Logga in" /></TD>
        </TR>
        -->
    </TABLE>

</form>
我的userbundle现在有这个类

<?php
namespace MyApp\UserBundle;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
{
   public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if(true === $request->isXmlHttpRequest()) {
            return new Response("success");
        }

        //default redirect operation.
        return parent::onAuthenticationSuccess($request, $token);
    }

}
我得到的错误是

RuntimeException: The parent definition "security.authentication.success_handler" defined for definition "my.authentication.success_handler" does not exist.

它是否与格式有关,在xml/yml中是否有所不同?我也不是这方面的专家,所以…

您可以使用防火墙配置的
成功处理程序
失败处理程序
选项。比如说

成功处理程序类

namespace Your\NameSpace;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
{
   public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if(true === $request->isXmlHttpRequest()) {
            return new Response("success");
        }

        //default redirect operation.
        return parent::onAuthenticationSuccess($request, $token);
    }

}
namespace Your\NameSpace;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        if(true === $request->isXmlHttpRequest()) {
            return new Response("failure");
        }

        //default redirect operation.
        return parent::onAuthenticationFailure($request, $exception);

    }
}
失败处理程序类

namespace Your\NameSpace;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
{
   public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if(true === $request->isXmlHttpRequest()) {
            return new Response("success");
        }

        //default redirect operation.
        return parent::onAuthenticationSuccess($request, $token);
    }

}
namespace Your\NameSpace;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        if(true === $request->isXmlHttpRequest()) {
            return new Response("failure");
        }

        //default redirect operation.
        return parent::onAuthenticationFailure($request, $exception);

    }
}
服务定义(xml)

最后在
security.yml

security:
    firewall:
        your_firewall:
            #...
            form_login:
                #...
                success_handler: my.authentication.success_handler
                failure_handler: my.authentication.failure_handler

我最终使用了这个解决方案(多亏了m2mdas&)

config.yml

#My services
services:
    my.authentication.success_handler:
        class:     Hemekonomi\UserBundle\AuthenticationSuccessHandler
        parent:    security.authentication.success_handler  
#My services
parameters:
    security.authentication.success_handler.class: MyApp\UserBundle\Resources\AuthenticationSuccessHandler

services:
    security.authentication.success_handler:
        class: %security.authentication.success_handler.class%
        public: false
        arguments:  ['@router', '@security.user.entity_manager']
security.wml

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            success_handler: security.authentication.success_handler
        logout:       true
        anonymous:    true
successhandler(包括比我要求的功能稍多的功能,稍后可能会使用此功能):

router->generate('MyAppSkrivbordBundle_主页');
返回新的重定向响应($uri);
}
}

非常感谢,我想我快到了,这似乎正是我所需要的。我收到一个错误,但是,我已经更新了上面的代码以反映您的解决方案。或者,我可能没有将处理程序放在我的包中,它应该放在特定的位置吗?很抱歉,放置了xml配置。我已经扩展了默认的成功和失败处理程序。优点是,新服务遵循
securit.yml
中定义的重定向选项或普通页面的referer重定向。添加了yml服务配置。
#My services
parameters:
    security.authentication.success_handler.class: MyApp\UserBundle\Resources\AuthenticationSuccessHandler

services:
    security.authentication.success_handler:
        class: %security.authentication.success_handler.class%
        public: false
        arguments:  ['@router', '@security.user.entity_manager']
firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            success_handler: security.authentication.success_handler
        logout:       true
        anonymous:    true
<?php

namespace MyApp\UserBundle\Resources;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\EntityManager;


/**
 * Custom authentication success handler
 */
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{

   private $router;
   private $em;

   /**
    * Constructor
    * @param RouterInterface   $router
    * @param EntityManager     $em
    */
   public function __construct(RouterInterface $router, EntityManager $em)
   {
      $this->router = $router;
      $this->em = $em;
   }


   function onAuthenticationSuccess(Request $request, TokenInterface $token)
   {

        if(true === $request->isXmlHttpRequest()) {
            return new Response("success");
        }

        //default redirect operation.
        $uri = $this->router->generate('MyAppSkrivbordBundle_homepage');
        return new RedirectResponse($uri);


   }
}