简体   繁体   中英

Symfony2 ContextErrorException in Custom Authentication Provider

I'm trying to make a How to create a custom Authentication Provider , after a full reading and looking into the Symfony code, I thought that just with creating a Factory, Authentication Provider and using the symfony default class will be enough but actually I'm missing something and I'm getting this error

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

The Factory

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';
    }

}

My Provider

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.yml

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

need some help to figure this out, what I'm missing here? do I need to create a custom listener even if the default "security.authentication.listener.form" fulfil my needs?

You're passing a string arguments: [""] as first argument to the constructor of the service.

That's why the typehint in __construct(UserProviderInterface $userProvider) fails.

Inject a UserProviderInterface properly and the exception will disappear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM