FOSUserBundle redirect from login page after logged in

前端 未结 4 817
北恋
北恋 2020-12-02 15:05

I simply want that if admin user or front end user try to access login page even after logged in

/admin/login (admin user) 

OR



        
相关标签:
4条回答
  • 2020-12-02 15:14

    Just redirect in the controller of the page that you added in default_target_path to the wanted direction, for example, if you put for default_target_path: /index and index is an action defined in HomePageCOntroller, go to HomePageCOntroller, test if the current user is admin or not with:

    if (($this->container->get('security.context')->isGranted('ROLE_ADMIN'))) 
    

    and then redircet him to the admin space.

    0 讨论(0)
  • 2020-12-02 15:18

    You can override FOSUserBundle\Controller\SecurityController and add the following code at the top of loginAction.

    use Symfony\Component\HttpFoundation\RedirectResponse;
    
    // ...
    
    public function loginAction(Request $request)
    {
        $authChecker = $this->container->get('security.authorization_checker');
        $router = $this->container->get('router');
    
        if ($authChecker->isGranted('ROLE_ADMIN')) {
            return new RedirectResponse($router->generate('admin_home'), 307);
        } 
    
        if ($authChecker->isGranted('ROLE_USER')) {
            return new RedirectResponse($router->generate('user_home'), 307);
        }
    
        // ... 
    
    0 讨论(0)
  • 2020-12-02 15:21

    Redirecting on login/logout in Symfony2 using LoginHandlers

    You should implement the AuthenticationSuccessHandlerInterface to handle the last minute decision when the login success.

    Implement the AuthenticationSuccessHandlerInterface:

    <?php
    // AcmeBundle\Security\LoginSuccessHandler.php
    
    namespace AcmeBundle\Security;
    
    use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\Routing\Router;
    
    class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface {
    
        protected $router;
        protected $authorizationChecker;
    
        public function __construct(Router $router, AuthorizationChecker $authorizationChecker) {
            $this->router = $router;
            $this->authorizationChecker = $authorizationChecker;
        }
    
        public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
    
            $response = null;
    
            if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
                $response = new RedirectResponse($this->router->generate('backend'));
            } else if ($this->authorizationChecker->isGranted('ROLE_USER')) {
                $response = new RedirectResponse($this->router->generate('frontend'));
            }
    
            return $response;
        }
    
    }
    

    Register your class as a service:

    # app/config/services.yml
    
    services:
        authentication.handler.login_success_handler:
            class:  AcmeBundle\Security\LoginSuccessHandler
            arguments:  ['@router', '@security.authorization_checker']
    

    Add a reference to your LoginSuccessHandler class in the firewall

    # app/config/security.yml
    
    firewalls:
        main:
            pattern: ^/
                form_login:
                    success_handler: authentication.handler.login_success_handler     
    
    0 讨论(0)
  • 2020-12-02 15:32

    The easier solution is to add these two lines to your app/config/security.yml:

    always_use_default_target_path & default_target_path, e.g.:

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: /login
                check_path: /login_check
                always_use_default_target_path: false
                default_target_path:            /your/start/path/
    
    0 讨论(0)
提交回复
热议问题