gpt4 book ai didi

php - Symfony2 AJAX 登录

转载 作者:IT王子 更新时间:2023-10-29 00:01:43 27 4
gpt4 key购买 nike

我有一个示例,我尝试使用 Symfony2 和 FOSUserBundle 创建 AJAX 登录。我在 security.yml 文件的 form_login 下设置自己的 success_handlerfailure_handler

这是类(class):

class AjaxAuthenticationListener implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
/**
* This is called when an interactive authentication attempt succeeds. This
* is called by authentication listeners inheriting from
* AbstractAuthenticationListener.
*
* @see \Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener
* @param Request $request
* @param TokenInterface $token
* @return Response the response to return
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($request->isXmlHttpRequest()) {
$result = array('success' => true);
$response = new Response(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}

/**
* This is called when an interactive authentication attempt fails. This is
* called by authentication listeners inheriting from
* AbstractAuthenticationListener.
*
* @param Request $request
* @param AuthenticationException $exception
* @return Response the response to return
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($request->isXmlHttpRequest()) {
$result = array('success' => false, 'message' => $exception->getMessage());
$response = new Response(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
}

这对于处理成功和失败的 AJAX 登录尝试都非常有效。但是,启用后 - 我无法通过标准表单 POST 方法(非 AJAX)登录。我收到以下错误:

可捕获的 fatal error :传递给 Symfony\Component\HttpKernel\Event\GetResponseEvent::setResponse() 的参数 1 必须是 Symfony\Component\HttpFoundation\Response 的一个实例,给定 null

我希望我的 onAuthenticationSuccessonAuthenticationFailure 覆盖仅对 XmlHttpRequests(AJAX 请求)执行,如果没有,则简单地将执行交回原始处理程序.

有没有办法做到这一点?

TL;DR 我希望 AJAX 请求的登录尝试返回成功和失败的 JSON 响应,但我希望它不会影响通过表单 POST 进行的标准登录。

最佳答案

大卫的 answer很好,但它缺少一些新手的细节 - 所以这是为了填补空白。

除了创建 AuthenticationHandler 之外,您还需要使用创建处理程序的包中的服务配置将其设置为服务。默认的包生成会创建一个 xml 文件,但我更喜欢 yml。这是一个示例 services.yml 文件:

#src/Vendor/BundleName/Resources/config/services.yml

parameters:
vendor_security.authentication_handler: Vendor\BundleName\Handler\AuthenticationHandler

services:
authentication_handler:
class: %vendor_security.authentication_handler%
arguments: [@router]
tags:
- { name: 'monolog.logger', channel: 'security' }

您需要修改 DependencyInjection 包扩展以使用 yml 而不是 xml,如下所示:

#src/Vendor/BundleName/DependencyInjection/BundleExtension.php

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

然后在您应用的安全配置中设置对您刚刚定义的 authentication_handler 服务的引用:

# app/config/security.yml

security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
success_handler: authentication_handler
failure_handler: authentication_handler

关于php - Symfony2 AJAX 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607212/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com