multiple firewalls with symfony2

2019-02-01 21:24发布

问题:

How to have multiple firewalls with symfony2?

This is mentioned in the documentation but never explained.

回答1:

I finally found the answer. Here is the final working security.yml :

security:
    encoders:
        entity_admin:
            class: MyBundle\Entity\AdminUser
            algorithm: sha512
            iterations: 5000
            encode_as_base64: false
        entity_members:
            class: MyBundle\Entity\User
            algorithm: sha512
            iterations: 1000
            encode_as_base64: false

    providers:
        entity_admin:
            entity:
                class: MyBundle\Entity\AdminUser
                property: username
        entity_members:
            entity:
                class: MyBundle\Entity\User
                property: username

    firewalls:
        admin_secured_area:
            pattern: /admin/.*
            provider: entity_admin
            anonymous: ~
            form_login:
                check_path: /admin/login_check
                login_path: /admin/login
            logout:
                path:   /admin/logout
                target: /admin/
        members_secured_area:
            pattern: /members/.*
            provider: entity_members
            anonymous: ~
            form_login:
                check_path: /members/login_check
                login_path: /members/login
            logout:
                path:   /members/logout
                target: /members/

    access_control:
        admin_login:
            path: /admin/login
            roles: IS_AUTHENTICATED_ANONYMOUSLY
        admin_area:
            path: /admin/.*
            roles: ROLE_ADMIN
        members_login:
            path: /members/login
            roles: IS_AUTHENTICATED_ANONYMOUSLY
        members_register:
            path: /members/register
            roles: IS_AUTHENTICATED_ANONYMOUSLY
        members_area:
            path: /members/.*
            roles: ROLE_USER

And a pastebin



回答2:

In your yml config file put:

security:
    firewalls:
        filrewall_1:
            ...
        filrewall_2:
            ...

Post a comment if you have problems.

See also : http://symfony.com/doc/current/reference/configuration/security.html



回答3:

You can have users authenticate into one firewall and be authenticated on others by configuring the same firewall context. Take for example this app/config/security.yml:

security:
    firewalls:
        filrewall_1:
            ...
            context: my_context
        filrewall_2:
            ...
            context: my_context

http://symfony.com/doc/current/reference/configuration/security.html#firewall-context