symfony - 开发时发生 fatal error onAuthenticationFailure

标签 symfony

我正在 symfony2 中使用 ajax 和 FOSUserBundle 实现身份验证。这是我在 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方法单独行走

最佳答案

您错过了 AuthenticationException 的使用声明.

添加use Symfony\Component\Security\Core\Exception\AuthenticationException;到顶部

关于symfony - 开发时发生 fatal error onAuthenticationFailure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11393266/

相关文章:

php - PSR-4 代码库中条令生成器的解决方法

symfony - Composer 忽略最小稳定

用于更改数组的 "order"的 PHP 算法

php - 如何在 1 个版本的库中处理 2 个版本的依赖项

php - 如何不使用 Doctrine 刷新 Symfony2 中的持久实体?

php - 如何仅针对某个 Controller 操作禁用实体缓存到 Redis?

php - 如何在 symfony 3.4 中正确验证 JSON 请求

security - 禁用重定向 symfony 安全

php - 在 Symfony 中出现第一个错误时停止对象属性验证有多漂亮?

php - Doctrine 十进制在查询中不存在