如何在Symfony 2中的login_check之后禁用redirect
我需要在login检查后禁用redirect,因为我只需要获取login成功与否。 提交/ login_check url后给我正确的数据,但保持redirect到/login(失败)。
/login后空白。
我正在尝试使用extjs 4设置login表单,所以我需要通过ajax post请求进行validation。 login_check应该进行身份validation,创build用户会话并返回它是成功还是失败,但是不能在任何地方转发。
我的login.html.twig看起来像:
{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %} { success:true } {% else %} { success: false } {% endif %}
和security.yml中:
firewalls: main: form_login: provider: fos_userbundle failure_path: null failure_forward: false
创build一个authentication处理器:
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 } } }
将处理程序注册为服务:
services: authentication_handler: class: YourVendor\UserBundle\Handler\AuthenticationHandler
在防火墙中注册服务:
firewalls: main: form_login: success_handler: authentication_handler failure_handler: authentication_handler
这是一个粗略的例子给你的一般想法 – 你需要自己弄清楚细节。 如果您遇到困难,需要进一步澄清,请在评论中提出您的问题,我将尽力详细说明该示例。
正常的symfonystream程是redirect到一个login页面,如果你没有login,这对人类工作正常。 但是你似乎在寻找一个程序化的解决scheme。
你有没有尝试在窗体中设置_target_path来声明“下一页”应该是什么? Symfony总是会在某个地方转发给你,但是你可以把它设置在任何你想要的地方。
我发现这两个页面对描述login表单的内部工作非常有用:
- 如何自定义表单login (search_target_path)
- 手册中的安全页面