Symfony 重写FOSUserBundle LOGINATION()以重定向到自定义路由

Symfony 重写FOSUserBundle LOGINATION()以重定向到自定义路由,symfony,fosuserbundle,Symfony,Fosuserbundle,我正在与FOSUserBundle合作一个Symfony 3.3.8项目。我已经为学生和提供者分别创建了两个注册页面和两个个人资料页面。现在我试图覆盖FOSUserBundle的loginAction()方法,在这里我检查登录用户的角色。如果角色是role\u STUDENT我尝试在成功登录后将用户重定向到学生配置文件页面,如果角色是role\u PROVIDER我希望在成功登录后将用户重定向到提供商配置文件页面。这是我用loginAction()重写的SecurityController: 以

我正在与FOSUserBundle合作一个Symfony 3.3.8项目。我已经为学生和提供者分别创建了两个注册页面和两个个人资料页面。现在我试图覆盖FOSUserBundle的loginAction()方法,在这里我检查登录用户的角色。如果角色是
role\u STUDENT
我尝试在成功登录后将用户重定向到学生配置文件页面,如果角色是
role\u PROVIDER
我希望在成功登录后将用户重定向到提供商配置文件页面。这是我用
loginAction()重写的SecurityController


以下是一些可能对您有所帮助的代码

1-这是事件/登录成功处理程序,您的逻辑在此类中

<?php

namespace AppBundle\Event;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;


class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
    protected $router;
    protected $security;

    public function __construct(Router $router, AuthorizationChecker $security)
    {
        $this->router   = $router;
        $this->security = $security;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $url = 'homepage';

        if ($this->security->isGranted('ROLE_STUDENT')) {
            $url = 'student_route';
        }
        if ($this->security->isGranted('ROLE_PROVIDER')) {
            $url = 'provider_route';
        }

        $response = new RedirectResponse($this->router->generate($url));

        return $response;
    }
}

3-应该是这样。试着告诉我是否出了问题

我不认为覆盖是最好的解决方案。您应该使用侦听器捕获登录事件,然后重定向用户。这可能会有帮助(未经测试):我认为唯一缺少的是security.yml中的
表单\u login:success\u handler:authentication.handler.login\u success\u handler
<?php

namespace AppBundle\Event;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;


class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
    protected $router;
    protected $security;

    public function __construct(Router $router, AuthorizationChecker $security)
    {
        $this->router   = $router;
        $this->security = $security;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $url = 'homepage';

        if ($this->security->isGranted('ROLE_STUDENT')) {
            $url = 'student_route';
        }
        if ($this->security->isGranted('ROLE_PROVIDER')) {
            $url = 'provider_route';
        }

        $response = new RedirectResponse($this->router->generate($url));

        return $response;
    }
}
login_success_handler:
    class: AppBundle\Event\LoginSuccessHandler
    arguments:
        - "@router"
        - "@security.authorization_checker"
        tags:
            - { name: 'monolog.logger', channel: 'security' }