Symfony 3.4记住我重启浏览器后无法使用

时间:2018-07-25 14:03:18

标签: symfony

我不使用FOSUserBUndle手动设置了带有symfony的登录功能,我想添加选项“记住我”功能

一切正常,cookie的创建方式很好,只是当我离开浏览器并重新打开它时,我仍然没有连接

这是我的代码:

public function connect(array $arrayUser, array $roles)
{

    if (!isset($user['secretKey'])) {
        $credentialsClient = $this->get('custom.security.credentials_client');
        $accessToken       = $credentialsClient->getAccessToken();
        $secretKey         = $credentialsClient->fetch('/security/'.$arrayUser['id']);
    } else {
        $secretKey         = $arrayUser['secretKey'];
    }
    $user = new User();
    $user->setId($arrayUser['id']);
    $user->setRoles($roles);
    $user->setUsername($arrayUser['username']);
    $request = $this->get('request_stack')->getCurrentRequest();



    if ($request->get('_remember_me') == 'on'){
        //Token to set the REMEMBERME cookie
        $token = new UsernamePasswordToken($user, $user->getPassword(), 'secured_area', $roles);

        //Token to set the session
        $token_session = new CustomToken($user->getId(), $secretKey, $roles);
        $this->get('security.token_storage')->setToken($token_session);
        $this->get('session')->set('apiUser', serialize($arrayUser));
       $rememberMeService = new TokenBasedRememberMeServices([$arrayUser],$this->getParameter('secret'),'secured_area', [
        'path' => '/',
        'name' => 'REMEMBERME',
        'domain' => 'localhost',
        'secure' => false,
        'httponly' => true,
        'lifetime' => 1209600, // 14 days
        'always_remember_me' => false,
        'remember_me_parameter' => '_remember_me'
      ]);
       $response = new Response();
       $rememberMeService->loginSuccess($request, $response, $token);

       $event   = new InteractiveLoginEvent($request, $token);
       $response->sendHeaders();
       $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
       return $response;


    }else{
        $token = new CustomToken($user->getId(), $secretKey, $roles);
        $this->get('security.token_storage')->setToken($token);
        $this->get('session')->set('apiUser', serialize($arrayUser));

        // Now dispatch the login event

        $event   = new InteractiveLoginEvent($request, $token);
        $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
    }

} 

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我像这样更改这部分代码:

 if ($request->get('_remember_me') == 'on'){
    //Token to set the REMEMBERME cookie
    $token = new UsernamePasswordToken($user->getId(), $user->getPassword(), 'secured_area', $roles);


    $this->get('security.token_storage')->setToken($token);
    $this->get('session')->set('apiUser', serialize($arrayUser));
   $rememberMeService = new TokenBasedRememberMeServices([$arrayUser],$this->getParameter('secret'),'secured_area', [
    'path' => '/',
    'name' => 'REMEMBERME',
    'domain' => 'localhost',
    'secure' => false,
    'httponly' => true,
    'lifetime' => 1209600, // 14 days
    'always_remember_me' => false,
    'remember_me_parameter' => '_remember_me'
  ]);

当我使用UsernamePasswordToken令牌登录但未设置cookie时,我已经建立了联系,因为$ user不代表UserInterface实现

那是我的问题,我没有使用FOSUserBundle或Entity来加载用户,而是通过API来加载用户

如果我这样做:

if ($request->get('_remember_me') == 'on'){
    //Token to set the REMEMBERME cookie
    $token = new UsernamePasswordToken($user, $user->getPassword(), 'secured_area', $roles);


    $this->get('security.token_storage')->setToken($token);
    $this->get('session')->set('apiUser', serialize($arrayUser));
   $rememberMeService = new TokenBasedRememberMeServices([$arrayUser],$this->getParameter('secret'),'secured_area', [
    'path' => '/',
    'name' => 'REMEMBERME',
    'domain' => 'localhost',
    'secure' => false,
    'httponly' => true,
    'lifetime' => 1209600, // 14 days
    'always_remember_me' => false,
    'remember_me_parameter' => '_remember_me'
  ]);

我收到此错误:没有用户“ UserBundle \ Model \ User”的用户提供程序。