Security - After configuring symfony2 firewall, how to obtain current user information on unauthorized pages?
伊谢尔伦
伊谢尔伦 2017-05-16 16:45:23
0
1
504

The firewall configuration is as follows:

firewalls: 
     login_firewall: 
         pattern:    ^/login$ 
         anonymous:  ~ 
     secured_area: 
         pattern:    ^/ 
         login_form: ~ 

After logging in, except for the /login page, other pages can obtain the current user information through $this->getUser(),
How can I get the current user information on the /login page?


I solved the problem based on the answer. Here is my security.yml content:

security:
role_hierarchy:
    ROLE_ADMIN: ROLE_USER

firewalls:
    # login_firewall:
    #     pattern:    ^/login$
    #     anonymous:  ~
    secured_area:
        pattern:    ^/
        anonymous:  ~
        form_login:
            login_path: login
            check_path: login_check
        logout:
            path:   /logout
            target: /

access_control:
    - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: ROLE_USER }

providers:
    in_memory:
        memory:
            users:
                kevin:  { password: kevinpass, roles: 'ROLE_USER' }
                admin:  { password: adminpass, roles: 'ROLE_ADMIN' }

encoders:
    Symfony\Component\Security\Core\User\User: plaintext

At the same time, I also found a similar question on SO - link
Thanks for the answer friends!

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
phpcn_u1582

You can delete the login firewall and configure it in acl:

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template