如果用户登录,我想调用AuthenticationSuccessHandler . 但是如果我使用正确的密码尝试它,则会发生错误:

request.INFO:匹配路由“login_check”(参数:“_ control”:“AppBundle \ Controller \ SecurityController :: loginCheckAction”,“_ route”:“login_check”)[] [] request.CRITICAL:未捕获PHP异常Symfony \ Component \ Debug \ Exception \ ContextErrorException:“Catchable Fatal Error:Argument 4传递给Symfony \ Component \ Security \ Core \ Authentication \ Token \ UsernamePasswordToken :: __ construct()必须是类型数组,字符串给定,在/ home / jail中调用/home/users/requiem/rpg/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php在第96行并定义为“at / home / jail / home / users / requiem / rpg / vendor / symfony / symfony / src / Symfony / Component / Security / Core / Authentication / Token / UsernamePasswordToken.php第36行{“exception”:“[object](Symfony \ Component \ Debug \ Exception \ ContextErrorException(code:0) ):可捕获的致命错误:参数4传递给Symfony \ Component \ Security \ Core \ Authentication \ Token \ UsernamePasswordToken :: __ construct()必须是类型数组,给定字符串,在第96行/home/jail/home/users/requiem/rpg/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php中调用并定义于/home/jail/home/users/requiem/rpg/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php:36)“} [] security.DEBUG:在session [] []中写SecuritySetext

我的文件:

security.yml

firewalls:
    secured_area:
        pattern: ^/
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login_check
            always_use_default_target_path: true
            default_target_path: /rpg/main
            username_parameter: _username
            password_parameter: _password
            success_handler: security.authentication.success_handler

services.yml

security.authentication.success_handler:
        class: AppBundle\Handler\AuthenticationSuccessHandler
        arguments: [@security.http_utils, @service_container, {}]
        tags:
            - { name: 'monolog.logger', channel: 'security'}

src/AppBundle/Handler/AuthenticationSuccessHandler

namespace AppBundle\Handler;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler{
protected $container;

public function __construct(HttpUtils $httpUtils, ContainerInterface $cont, array $options)
{
    parent::__construct($httpUtils, $options);
    $this->container=$cont;
}

public function onAuthenticationSuccess(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token)
{
    $user=$token->getUser();
    $user->setLogged(new \DateTime());

    $em=$this->container->get('doctrine.orm.entity_manager');

    $em->persist($user);
    $em->flush();

    return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
}
}