在Symfony 2中的login_check之后如何禁用重定向 [英] How to disable redirection after login_check in Symfony 2
问题描述
我需要在登录检查后禁用重定向,因为我仅需要获得登录成功与否的信息.提交/login_check网址后,请提供正确的数据,但请继续重定向到/login(失败时).
/login之后为空.
我正在尝试使用extjs 4设置登录表单,因此我需要通过ajax发布请求进行验证.
login_check应该进行身份验证,创建用户会话并返回成功还是失败,但无处转发.
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.html.twig看起来像:
my login.html.twig looks like:
{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
{ success:true }
{% else %}
{ success: false }
{% endif %}
并在security.yml中:
and in security.yml:
firewalls:
main:
form_login:
provider: fos_userbundle
failure_path: null
failure_forward: false
推荐答案
创建身份验证处理程序:
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
在防火墙中注册服务:
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.
这篇关于在Symfony 2中的login_check之后如何禁用重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!