The problem is you restricted access to /.*
, which means all paths, to only users who have the role ROLE_USER.
Say your login path is /login
, the user tries to access any other path and is redirected to the login path. The login path (/login
) will be matched by the access control pattern /.*
. The user will then be denied of access because he doesn't have the role ROLE_USER right now. The security component will redirect the user again to the login form so he can authenticate to get the role, which is restricted, and will redirect the user to the login form to authenticate and so on.
Here's a simple solution to avoid this problem. You can allow access to the login form to anonymous user with the activation of the anonymous user configuration and a new access control item. Add this below main
in the firewalls
configuration to enable anonymous user:
security:
firewalls:
main:
anonymous: true
And add a new access control item to allow anonymous user to acces the /login
pattern:
access_control:
- { path: /login, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /.*, role: ROLE_USER }
The order is important here since the rule is: first path matched wins. So the /login
path must be above your pattern for other path /.*
. This should resolves you redirect loop.
The documentation of Symfony about security is being rewritten right now and will talk more in details about this problem. It is in the symfony-docs github repository under the security branch.
Regards,
Matt
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…