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

nee*_*raj 30 redirect login symfony fosuserbundle

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

/admin/login (admin user) 
Run Code Online (Sandbox Code Playgroud)

要么

/login (front end user)
Run Code Online (Sandbox Code Playgroud)

那么他们应该像/admin或那样被重定向回相关的主页/

Ben*_*ten 64

更简单的解决方案是将这两行添加到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/
Run Code Online (Sandbox Code Playgroud)


Nez*_*dle 43

使用LoginHandlers在Symfony2中重定向登录/注销

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

}
Run Code Online (Sandbox Code Playgroud)

将您的课程注册为服务:

# app/config/services.yml

services:
    authentication.handler.login_success_handler:
        class:  AcmeBundle\Security\LoginSuccessHandler
        arguments:  ['@router', '@security.authorization_checker']
Run Code Online (Sandbox Code Playgroud)

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

# app/config/security.yml

firewalls:
    main:
        pattern: ^/
            form_login:
                success_handler: authentication.handler.login_success_handler     
Run Code Online (Sandbox Code Playgroud)


Nic*_*ich 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);
    }

    // ... 
Run Code Online (Sandbox Code Playgroud)

  • @venimous我在谈论**覆盖**控制器在这里......不直接修改第三方代码.在低估答案之前...下次请确保你真正理解它们.请阅读文档章节["如何覆盖捆绑包的任何部分"](http://symfony.com/doc/current/cookbook/bundles/override.html),谢谢和快乐的编码! (3认同)