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

Phi*_*use 7 php authentication service dependency-injection symfony

我正在关注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 }
Run Code Online (Sandbox Code Playgroud)

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

小智 8

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


Eln*_*mov 6

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

services:
    in_memory_user_provider:
        alias: security.user.provider.concrete.in_memory
Run Code Online (Sandbox Code Playgroud)

现在,您可以访问它in_memory_user_provider.

  • 两个答案都是相似的,所以我已经接受了第一个答案,但感谢您提供使用别名的更多信息。 (2认同)