我有一个扩展fos用户包和另一个包的包.
我希望一旦用户通过身份验证,根据他的角色管理员或简单用户将他重定向到不同的视图.
我的问题是我找不到登录的控制器,我将从哪里进行重定向.
角色是User来自数据库的实体的属性.
Ahm*_*ani 10
您需要添加一个实现AuthenticationSuccessHandler接口的LoginSuccessHandler ,
然后,您可以在onAuthenticationSuccess()方法中设置重定向逻辑,如下所示,
namespace XXX\YourBundler\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
public function __construct(Router $router, SecurityContext $security)
{
$this->router = $router;
$this->security = $security;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($this->security->isGranted('ROLE_XXXX_1'))
{
$response = new RedirectResponse($this->router->generate('route_1'));
}
elseif ($this->security->isGranted('ROLE_XXXX_2'))
{
$response = new RedirectResponse($this->router->generate('route_2'));
}
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
您的处理程序也必须注册为服务,
parameters:
security.authentication.success_handler.class: XXX\YourBundler\Handler\AuthenticationSuccessHandler
services:
security.authentication.customized_success_handler:
class: %security.authentication.success_handler.class%
public: false
arguments: [@router, @security.context]
Run Code Online (Sandbox Code Playgroud)
然后,您可以将以下行添加到防火墙安全配置中,
success_handler: security.authentication.customized_success_handler
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6931 次 |
| 最近记录: |