在 JSON 身份验证之前侦听请求 symfony 4



我必须拖着方式将用户登录到我的应用程序:从后台或从api。 出于某种原因,我想在身份验证过程开始之前处理 LDAP 检查。

  • 对于BO,我发现了这一点,并且它的工作很好:

在身份验证之前侦听请求或在 Symfony3 中重写/login_check

  • 我也想为 api json 登录执行此操作,所以我想扩展用户名密码Json身份验证侦听器

但是当我尝试登录API时,我遇到了此错误:

传递给 Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener::__construct(( 的参数 3 必须是 Symfony\Component\Security\Http\HttpUtils 的实例,Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy 的实例

这是我的代码

服务.yaml

security.authentication.listener.json:
class: AppEventListenerUsernamePasswordJsonAuthenticationListener
parent: security.authentication.listener.abstract
abstract: true
autowire: true
autoconfigure: false
calls: [ [initialize, ["@doctrine.orm.entity_manager"]] ]

//UsernamePasswordJsonAuthenticationListener.php

<?php

namespace AppEventListener;
use DoctrineORMEntityManagerInterface;
use FOSUserBundleModelUserManagerInterface;
use AppServiceLdapTools;
use SymfonyComponentSecurityHttpFirewallUsernamePasswordJsonAuthenticationListener as baseJsonAuthenticationListener;
class UsernamePasswordJsonAuthenticationListener extends baseJsonAuthenticationListener
{
/** @var EntityManagerInterface */
protected $entityManager;
/** @var LdapTools */
protected $ldapTools;
/** @var UserManagerInterface */
protected $userManager;
/**
* setter is called through DI container
*
* @param EntityManagerInterface $entityManager
*/
public function initialize(EntityManagerInterface $entityManager,LdapTools $ldapTools,UserManagerInterface $userManager)
{
$this->entityManager = $entityManager;
$this->ldapTools = $ldapTools;
$this->userManager = $userManager;
}
}
?>

感谢之前.

我通过在 services.yml 中更改我的声明来修复它

security.authentication.listener.json:
class: AppEventListenerUsernamePasswordJsonAuthenticationListener
abstract: true
autowire: true
autoconfigure: false
arguments:
- "@security.token_storage"
- "@security.authentication.manager"
- "@security.http_utils"
- ~
- SymfonyComponentSecurityHttpAuthenticationAuthenticationSuccessHandlerInterface
- SymfonyComponentSecurityHttpAuthenticationAuthenticationFailureHandlerInterface
- ~
calls: [ [initialize, ["@doctrine.orm.entity_manager"]] ]

最新更新