我尝试将 FosUserBundle 与两个用户界面一起使用:
前端:/后端:/admin/
所以我使用LoginHandler。
我有一个扩展 FosUserBundle 的包(CulturalStore/UserBundle),在它的 services.xml 中:
parameters:
cultural_store_user.component.authentication.handler.logout_success_handler.class: CulturalStore\UserBundle\Component\Authentication\Handler\LogoutSuccessHandler
cultural_store_user.component.authentication.handler.login_success_handler.class: CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler
services:
cultural_store_user.component.authentication.handler.login_success_handler:
class: %cultural_store_user.component.authentication.handler.login_success_handler.class%
arguments: [@service_container, @router, @security.context]
tags:
- { name: 'monolog.logger', channel: 'security' }
cultural_store_user.component.authentication.handler.logout_success_handler:
class: %cultural_store_user.component.authentication.handler.logout_success_handler.class%
arguments: [@service_container, @router]
tags:
- { name: 'monolog.logger', channel: 'security' }
在 LoginSuccessHandler 我有:
<?php
namespace CulturalStore\UserBundle\Component\Authentication\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_SUPER_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_ADMIN'))
{
$response = new RedirectResponse($this->router->generate('category_index'));
}
elseif ($this->security->isGranted('ROLE_USER'))
{
// redirect the user to where they were before the login process begun.
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
}
return $response;
}
}
在 LogoutSuccesHandler 中:
<?php
namespace CulturalStore\UserBundle\Component\Authentication\Handler;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
protected $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function onLogoutSuccess(Request $request)
{
// redirect the user to where they were before the login process begun.
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
return $response;
}
}
最后,在全球 security.yml 我有:
firewalls:
main:
pattern: ^/
anonymous: true
provider: main
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
use_referer : true
success_handler: cultural_store_user.component.authentication.handler.login_success_handler
failure_handler: cultural_store_user.component.authentication.handler.login_success_handler
logout:
path: fos_user_security_logout
target: /
success_handler: cultural_store_user.component.authentication.handler.logout_success_handler
remember_me:
key: %secret%
但是,当我去网站时,我有这个错误:
FatalErrorException: Error: Class 'CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler' not found in /Users/Juju/Sites/workspace/CS/app/cache/dev/appDevDebugProjectContainer.php line 428
在 appDevDebugProjectContainer 第 428 行我有这个:
/**
* Gets the 'cultural_store_user.component.authentication.handler.login_success_handler' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler A CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler instance.
*/
protected function getCulturalStoreUser_Component_Authentication_Handler_LoginSuccessHandlerService()
{
return $this->services['cultural_store_user.component.authentication.handler.login_success_handler'] = new \CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler($this, $this->get('router'), $this->get('security.context'));
}