如何将内存中的用户提供程序注入服务?

时间:2012-10-03 15:42:47

标签: php service symfony dependency-injection authentication

我正在关注http://symfony.com/doc/current/cookbook/security/voters.html并尝试制作一个自定义投票人,拒绝访问不包含有效API密钥和摘要的请求(受http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html影响 - 我是没有构建身份验证提供程序,因为我需要为同一请求使用FOSUserBundle提供程序。)

我想将我的api密钥/秘密存储在内存中的用户提供程序中,并且可能会在以后将其迁移到自定义mongodb提供程序。所以我需要一种方法将用户提供者注入我的选民。我已经注入了服务容器,但可以从中访问用户提供程序吗?

我的服务定义:

services:
    security.access.api_client_voter:
        class:     Acme\RestBundle\Security\Authorization\Voter\ApiClientVoter
        arguments: [@service_container, %kernel.cache_dir%/security/nonces]
        public:    false
        tags:
            - { name: monolog.logger, channel: authentication }
            - { name: security.voter }

所以我的问题是,如何注入内存提供商?食谱中的WSSE示例似乎使用auth提供程序工厂来替换字符串'security.providers.in_memory',但由于我只是使用选民,这是必要的吗?如果有必要,我的工厂会是什么样子?

3 个答案:

答案 0 :(得分:8)

内存中的用户提供程序首先在SecurityBundle的security.xml中定义为抽象服务。根据您在security.yml中的用户提供程序配置,SecurityBundle的SecurityExtension然后创建具体的内存中用户提供程序服务,每个用户作为服务添加到该服务。如您所见,此服务的名称为security.user.provider.concrete.[name-of-your-firewall]。因此,应该可以使用此服务或将此服务注入您的选民。您始终可以查看/app/cache目录中的转储服务容器,查找服务名称以及是否已定义服务名称。

答案 1 :(得分:6)

内存中用户提供商服务的名称为security.user.provider.concrete.in_memory,但该服务是私有的,因此您需要在config.yml中为其定义别名:

services:
    in_memory_user_provider:
        alias: security.user.provider.concrete.in_memory

现在,您可以in_memory_user_provider访问它。

答案 2 :(得分:0)

该服务已经宣布

<service id="security.user.provider.in_memory" class="Symfony\Component\Security\Core\User\InMemoryUserProvider" abstract="true" />
相关问题