我正在使用 ajax 和FOSUserBundle
symfony2 实现身份验证。这是我在 security.yml 中的代码:
main:
pattern: ^/
form_login:
provider: fos_userbundle
default_target_path: hello
remember_me: true
success_handler: my.success_handler
failure_handler: my.success_handler
check_path: /login_check
login_path: /login
remember_me:
key: %secret%
anonymous: false
logout: true
和 services.yml:
my.success_handler:
class: Kgr\UserBundle\Controller\SuccessHandler
arguments: [@router]
和类 SuccessHandler :
namespace Kgr\UserBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class SuccessHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if($request->isXmlHttpRequest()){
$response = new Response(json_encode(array(
'success'=> 1,
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}else{
if ($token->getUser()->isSuperAdmin()) {
return new RedirectResponse($this->router->generate('admin'));
}
else {
return new RedirectResponse($this->router->generate('MyAppmondeUserBundle_homepage'));
}
}
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if($request->isXmlHttpRequest()){
$response = new Response(json_encode(array(
'success'=> 0,
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}else{
if ($token->getUser()->isSuperAdmin()) {
return new RedirectResponse($this->router->generate('admin'));
}
else {
return new RedirectResponse($this->router->generate('MyAppmondeUserBundle_homepage'));
}
}
}
}
我不明白错误:
Fatal error: Declaration of Kgr\UserBundle\Controller\SuccessHandler::onAuthenticationFailure() must be compatible with that of Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface::onAuthenticationFailure() in D:\wamp1\www\Symfony\src\Kgr\UserBundle\Controller\SuccessHandler.php on line 12
通过 onAuthenticationSuccess 单独行走的方法