[英]How to make /login route accessible only for anonymous users in Symfony4?
My access_controll
looks like:我的access_controll
看起来像:
- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/, roles: ROLE_USER}
I need to give an access to route /login
only to anonymously authenticated users.我只需要向匿名身份验证的用户授予路由/login
的访问权限。
好的,我找到了解决方案:
- { path: ^/login, allow_if: 'is_anonymous()'}
Okay, there's a better variant to do this even with redirect.好的,即使使用重定向,也有更好的变体来做到这一点。 Firstable, you need to edit security.yaml
with this:首先,您需要使用以下内容编辑security.yaml
:
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
- { path: ^/, roles: ROLE_USER}
And then just add this to your SecurityController::login()
method:然后将其添加到您的SecurityController::login()
方法中:
if ($this->isGranted('ROLE_USER')) {
return new RedirectResponse(
$this->generateUrl('index')
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.