Force user login in Symfony 2

2019-07-20 17:03发布

Whenever I try to remove the anonymous: ~ configuration in security.yml, The system ends up returning an Error 310: Redirect loop.

This is the config so far:

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

1条回答
女痞
2楼-- · 2019-07-20 17:44

Try this:

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

See the doc http://symfony.com/doc/current/book/security.html#book-security-common-pitfalls

Be sure the login page isn't secure

Also, be sure that the login page does not require any roles to be viewed. For example, the following configuration - which requires the ROLE_ADMIN role for all URLs (including the /login URL), will cause a redirect loop:

access_control:
    - { path: ^/, roles: ROLE_ADMIN }

Removing the access control on the /login URL fixes the problem:

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

Also, if your firewall does not allow for anonymous users, you'll need to create a special firewall that allows anonymous users for the login page:

firewalls:
    login_firewall:
        pattern:    ^/login$
        anonymous:  ~
    secured_area:
        pattern:    ^/
        form_login: ~
查看更多
登录 后发表回答