问题描述
我需要在登录检查后禁用重定向,因为我只需要知道登录是否成功.提交后/login_check url 给我正确的数据,但继续重定向到/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 YourVendorUserBundleHandler;
// "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: YourVendorUserBundleHandlerAuthenticationHandler
在防火墙中注册服务:
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 后禁用重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2