自定义身份验证提供程序中的Symfony2 ContextErrorException

卡斯特罗·罗伊(Castro Roy)

在尝试阅读完并仔细阅读Symfony代码后,我试图制作一个如何创建自定义身份验证提供程序的方法,我认为仅创建一个工厂,身份验证提供程序并使用symfony默认类就足够了,但实际上我我错过了一些东西,我得到这个错误

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Acme\DemoBundle\Provider\MyProvider::__construct() must implement interface Symfony\Component\Security\Core\User\UserProviderInterface, string given, called in D:\wamp\www\sf2ldap\app\cache\dev\appDevDebugProjectContainer.php on line 1383 and defined in D:\wamp\www\sf2ldap\src\Acme\DemoBundle\Provider\MyProvider.php line 20

该工厂

namespace Acme\DemoBundle\Factory;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory;

class MyFactory extends AbstractFactory
{

    public function getPosition()
    {
        return 'form';
    }

    public function getKey()
    {
        return 'kstr';
    }

    protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
    {

        $providerId = 'security.authentication.provider.kstr.' . $id;
        $container
                ->setDefinition($providerId, new DefinitionDecorator('kstr.security.authentication.provider'))
                ->replaceArgument(0, new Reference($userProviderId));
        return $providerId;
    }

    protected function getListenerId()
    {
        return 'security.authentication.listener.form';
    }

}

我的提供者

namespace Acme\DemoBundle\Provider;

use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class MyProvider implements AuthenticationProviderInterface
{

    private $_userProvider;

    public function __construct(UserProviderInterface $userProvider)
    {
        $this->_userProvider = $userProvider;
    }

    public function authenticate(TokenInterface $token)
    {
        try
        {
            $user = $this->_userProvider->loadUserByUsername($token->getUsername());
            //custom auth steps
            $token = new UsernamePasswordToken(
                    $token->getUsername(), null, $token->getProviderKey(), $user->getRoles()
            );
            return $token;
            }
        } catch (\Exception $exc)
        {
            throw new AuthenticationException('Invalid username or password. ', 0, $e);
        }
        throw new AuthenticationException('Invalid username or password asdfasd');
    }

    public function supports(TokenInterface $token)
    {
        return $token instanceof UsernamePasswordToken;
    }

}

services.yml

services:
    kstr.security.authentication.provider:
        class:  Acme\DemoBundle\Provider\MyProvider
        arguments: [""]

安全性

security:
    encoders:
        Acme\DemoBundle\Entity\SecureUser: plaintext
    providers:
        multiples:
            chain:
                providers: [entity_provider, ldap]
        entity_provider:
          entity: { class: AcmeDemoBundle:SecureUser, property: username }
        ldap:
          id: kstr.security.authentication.provider

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern:  ^/demo/secured/login$
            security: false

        secured_area:
            pattern:    ^/demo/secured/
            kstr: 
                check_path: _security_check
                login_path: _demo_login
                provider: ldap
            logout:
                path:   _demo_logout
                target: _demo

需要一些帮助来解决这个问题,我在这里想念的是什么?即使默认的“ security.authentication.listener.form”满足了我的需求,我仍需要创建一个自定义侦听器吗?

尼古拉·梅里(Nicolai Merry)

您正在将字符串 arguments: [""]作为第一个参数传递给服务的构造函数。

这就是为什么typehint__construct(UserProviderInterface $userProvider)失败的原因

UserProviderInterface正确注入后,异常会消失。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Symfony2 ContextErrorException

Prestashop 1.7 调度程序 ContextErrorException

在Symfony中生成捆绑后的ContextErrorException

Symfony2:ContextErrorException:可捕获的致命错误:传递给[...] :: __ construct()的参数1必须实现接口[...]

ContextErrorException:将复合键用于与Symfony 3实体进行学说关联时的未定义索引

注意:未定义的变量:eT1 500内部服务器错误-ContextErrorException

Symfony自定义身份验证提供程序根据请求重叠注销

Symfony自定义身份验证提供程序失败消息

如何通过Symfony 3身份验证使用自定义用户提供程序

PHP Symfony2自定义身份验证,无需数据库(SOAP)

Symfony2身份验证通过自定义UserProvider静默失败

未调用自定义身份验证提供程序

Spring Boot 2中针对Actuator和自定义API端点的单独身份验证提供程序

ArgumentNullException与ServiceStack中的自定义身份验证提供程序

在Oracle WebLogic Server 12.1.2.0.0中创建自定义身份验证提供程序

Spring Authorization Server 0.2.2,如何禁用(OAuth2TokenRevocation)之类的默认身份验证提供程序并用自定义的身份验证提供程序覆盖它?

Symfony2 phpunit功能测试自定义用户身份验证在重定向后失败(与会话相关)

无法为Spring安全性提供自定义身份验证提供程序

ASPNET Core 2中的自定义策略身份验证(jwt身份验证)

在 Azure Active Directory 中为自定义应用程序提供条件访问的多重身份验证

如何将自定义身份验证提供程序集成到IdentityServer4中

自动装配服务在Spring Security Java配置自定义身份验证提供程序中不起作用

Symfony2定制用户提供程序未进行身份验证/登录

我无法控制/无法访问的Firebase自定义身份验证提供程序?

Drupal 8外部/自定义身份验证提供程序

具有自定义身份验证提供程序的MobileIron SSO

让Spring Security选择实现自定义身份验证提供程序的类的实现

Spring-从控制器调用自定义身份验证提供程序

Spring Security自定义身份验证提供程序不起作用