【问题标题】:listen to request before json authentication symfony 4在 json 身份验证 symfony 4 之前监听请求
【发布时间】:2019-12-03 11:41:29
【问题描述】:

我有两种方法可以让用户登录我的应用程序:从后台或从 api。 出于某种原因,我想在身份验证过程开始之前处理 ldap 检查。

  • 对于 BO,我发现了这个,并且它的工作正常:

listen to request before authentication or rewrite /login_check in symfony3

  • 我也想为 api json 登录做这个,所以我想扩展 UsernamePasswordJsonAuthenticationListener

但当我尝试登录 api 时出现此错误:

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

这是我的代码

services.yaml

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

//用户名密码JsonAuthenticationListener.php

<?php


namespace App\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use App\Service\LdapTools;
use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener 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;
}

}

?>

感谢之前。

【问题讨论】:

    标签: symfony4


    【解决方案1】:

    我通过更改我在 services.yml 中的声明来解决这个问题

    security.authentication.listener.json:
        class: App\EventListener\UsernamePasswordJsonAuthenticationListener
        abstract: true
        autowire: true
        autoconfigure: false
        arguments:
                - "@security.token_storage"
                - "@security.authentication.manager"
                - "@security.http_utils"
                - ~
                - Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
                - Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface
                - ~
    
        calls: [ [initialize, ["@doctrine.orm.entity_manager"]] ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-04
      • 2023-03-20
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2019-05-14
      相关资源
      最近更新 更多