登录后,FOSUserBundle从登录页面重定向

时间:2013-09-13 09:56:15

标签: symfony redirect login fosuserbundle

如果管理员用户或前端用户在登录后尝试访问登录页面,我只是想要这样做

/admin/login (admin user) 

OR

/login (front end user)

然后他们应该被重定向回相关的主页,例如/admin/

4 个答案:

答案 0 :(得分:62)

更简单的解决方案是将这两行添加到app / config / security.yml:

always_use_default_target_path & default_target_path ,例如:

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/

答案 1 :(得分:43)

Redirecting on login/logout in Symfony2 using LoginHandlers

您应该实施AuthenticationSuccessHandlerInterface来处理登录成功时的最后一分钟决定。

实现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;
    }

}

将您的课程注册为服务:

# app/config/services.yml

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

在防火墙中添加对LoginSuccessHandler类的引用

# app/config/security.yml

firewalls:
    main:
        pattern: ^/
            form_login:
                success_handler: authentication.handler.login_success_handler     

答案 2 :(得分:22)

您可以覆盖FOSUserBundle\Controller\SecurityController并在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);
    }

    // ... 

答案 3 :(得分:3)

只需将您在default_target_path中添加的页面的控制器重定向到所需方向,例如,如果您输入default_target_path: /index并且index是HomePageCOntroller中定义的操作,转到HomePageCOntroller,测试当前用户是否为管理员:

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

然后将他重新加入管理空间。