[英]How to disable redirection after login_check in Symfony 2
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_check url给我正确的数据,但请继续重定向到/ login(失败)。
/login is blank after that. / login之后为空。
I am trying to set up login form using extjs 4 so I need to validate trough an ajax post request. 我正在尝试使用extjs 4设置登录表单,因此我需要通过ajax发布请求进行验证。 login_check should authenticate, create user session and return whether it was success or failure, but no forwarding anywhere. 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
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. 如果您遇到困难并且需要进一步说明,请将您的问题放在注释中,我将尝试详细说明该示例。
The normal symfony flow is to redirect you to a login page if you are not logged in, which works fine for humans. 正常的symfony流程是,如果您尚未登录,则将您重定向到登录页面,这对人类来说很好用。 But you seems to be looking for a programmatic solution. 但是您似乎正在寻找一种编程解决方案。
Have you tried setting _target_path in your form, to declare what the "next page" should be? 您是否尝试过在表单中设置_target_path来声明“下一页”应该是什么? Symfony is always going to forward you somewhere, but you can set that somewhere to wherever you want. Symfony总是会把您转发到某个地方,但是您可以将它设置到您想要的任何地方。
I found these two pages useful for describing the inner workings of the login form: 我发现这两页对于描述登录表单的内部工作很有用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.