从登录页面限制或重定向登录用户 [英] Restrict or redirect logged in users from login page

查看:29
本文介绍了从登录页面限制或重定向登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Symfony 2.7,安装了 FOSuserbundle,一切正常.我的用户正在创建,我可以用他们登录,但是登录的用户可以进入登录页面,这对我来说似乎不合逻辑.我找了一些答案,发现我必须配置我的 security.yml 文件,但它仍然不起作用,任何人都可以进入登录页面.

I am using Symfony 2.7, installed FOSuserbundle, everything is working fine. My users are being created and I can login with them, but logged in users can enter the log in page, which doesn't seem logic to me. I've looked a bit for answers and found out I have to configure my security.yml file, but it still doesn't work, anyone can enter the login page.

我发现我必须设置

 - { path: ^/, role: ROLE_USER}

但这给了我一个重定向循环.

but that gives me a redirect loop.

这是我所拥有的

    firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4

        logout:       true
        anonymous:    true
        # activate different ways to authenticate

        # http_basic: ~
        # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

        # form_login: ~
        # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/profile, role: ROLE_USER }
    - { path: ^/admin/, role: ROLE_ADMIN }

推荐答案

覆盖tge SecurityController的loginAction,这样:

Override the loginAction of tge SecurityController, this way:

class SecurityController extends BaseSecurityController
{
public function loginAction(Request $request)
{
   if( $this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') { 
   return $this->redirect($this->generateUrl('any_route_you_want'))
}

  return parent::loginAction($request);
 }
}

编辑:要了解如何覆盖 Bundle 的任何部分,这个 会很有帮助

Edit : To learn how to override any part of a Bundle, this would be helpful

这篇关于从登录页面限制或重定向登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆