duanke9540 2014-01-31 18:01
浏览 44
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dougu1990 2014-02-01 11:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题