Symfony 2 - firewall and access control issue

前端 未结 2 1209
误落风尘
误落风尘 2020-12-16 07:15

I\'ve got a wired problem with the symfony 2 security component. Due to the fact that the {{ app.user }} object is only available within the secured area, I set

相关标签:
2条回答
  • 2020-12-16 07:30

    Worth mentioning is that the best practice here is to use only one firewall with access_control for login page. Why? What would You do if the logged user tries to access the /login page? You won't be able to check in controller if he is authenticated and redirect him, because the user will be authenticated to your main firewall, but not to the login firewall, as they are separate security systems.

    Here is the security.yml that works great for me:

    security:
        firewalls:
            dev:
                pattern:  ^/(_(profiler|wdt)|css|images|js)/
                security: true
                anonymous: ~ 
            secured_area:
                pattern:    ^/
                anonymous:  ~
                form_login:
                    login_path:  /login
                    check_path:  /login_check
                    always_use_default_target_path: true
                    default_target_path: /
                logout:
                    path:   /logout
                    target: /
        providers:
            main:
                entity: { class: Core\UserBundle\Entity\User, property: username }
        access_control:
            - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/admin, roles: ROLE_SUPERADMIN }
            - { path: ^/user, roles: ROLE_USER }
            - { path: ^/, roles: IS_AUTHENTICATED_FULLY }
    
    0 讨论(0)
  • 2020-12-16 07:39

    USe anynymous directive in account_area:

    account_area:
            pattern:    ^/
            anonymous: ~
    
    0 讨论(0)
提交回复
热议问题