Symfony-在运行时配置防护身份验证

时间:2019-02-21 08:16:10

标签: php symfony authentication guard bundles

当前,我正在使用Symfony 4.2开发Web应用程序。

该应用程序需要模块化。所以我正在使用捆绑软件。目前,我在身份验证方面遇到问题。

假设有两个捆绑包:

  • CustomerLoginBundle
  • AdminBundle

两个捆绑包都使用防护来实现自己的身份验证。

该应用程序可以有多个用例。其中有些不会使用CustomerLoginBundle,有些会使用。

如何根据运行的上述捆绑包在运行时动态加载相应的安全配置?

示例:

两个软件包都已安装,安全配置应如下所示:

encoders:
    Acme\Bundle\AdminBundle\Entity\Admin:
        algorithm: bcrypt

    Acme\Bundle\CustomerLoginBundle\Entity\Customer:
        algorithm: bcrypt

providers:
    admin_provider:
        entity:
            class: Acme\Bundle\AdminBundle\Entity\Admin
            property: email
    customer_provider:
        entity:
            class: Acme\Bundle\CustomerLoginBundle\Entity\Customer
            property: email

    chain_provider:
        chain:
            providers: [customer_provider, admin_provider]

firewalls:
    main:
        provider: chain_provider
        anonymous: true
        guard:
            entry_point: Acme\Bundle\CustomerLoginBundle\Security\CustomerFormAuthenticator
            authenticators:
                - Acme\Bundle\AdminBundle\Security\AdminFormAuthenticator
                - Acme\Bundle\CustomerLoginBundle\Security\CustomerFormAuthenticator

仅安装了管理员捆绑包:

encoders:
    Acme\Bundle\AdminBundle\Entity\Admin:
        algorithm: bcrypt

providers:
    admin_provider:
        entity:
            class: Acme\Bundle\AdminBundle\Entity\Admin
            property: email

    chain_provider:
        chain:
            providers: [admin_provider]

firewalls:
    main:
        provider: chain_provider
        anonymous: true
        guard:
            authenticators:
                - Acme\Bundle\AdminBundle\Security\AdminFormAuthenticator

我的意思是我可以自己为每个用例手动设置配置,但这对我来说似乎是个懒惰的解决方案。

0 个答案:

没有答案