symfony 3.3以“记住我"功能以编程方式登录用户 [英] symfony 3.3 programmatically login a user with remember me feature

查看:157
本文介绍了symfony 3.3以“记住我"功能以编程方式登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在注册成功后登录用户.目前,我在网上找到的解决方案是按会话登录用户.但是我需要记住我"功能.我的研究使我进入

I want to login a user after registration successfully. Currently the solution I found online is login a user in a session basis. But I need the "remember me" feature. My research lead me to the

Symfony \ Component \ Security \ Http \ RememberMe \ TokenBasedRememberMeServices

Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices

及其功能

onLoginSuccess

onLoginSuccess

但是我很不走运,最终发现要进行服务的循环引用.同样,onLoginSuccess是受保护的函数.我假设我可以在执行登录功能的我自己的功能中复制这些功能.但这始终不是一个好选择.所以我想知道是否有人对此有经验?

But I'm out of luck and end up with circular reference detected for service. Also the onLoginSuccess is protected function. I assume I can duplicate those functions in my own function where performing a login feature. But that's always not a good choice. So I'd like to learn if anyone had experience on that?

推荐答案

在这里,我有自己的答案. 首先,使用以下代码段

Here I had my own answer. First, login user with following snippet

    $user = new \AppBundle\Security\User\EndUser($id, $userKey, $username, $password, $salt, $roles);
    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $this->container->get('security.token_storage')->setToken($token);
    $this->container->get('session')->set('_security_main', serialize($token));

请注意,您自己的userProvider或EndUser可能与我的不同.相应地更改$ user使其在您自己的情况下工作.

Be aware that your own userProvider or EndUser maybe different from mine. Change the $user accordingly to make it work in your own situation.

第二,实现记住我"功能.

Second, implement the remember me feature.

    $file   = sprintf("%s/config/security.yml", $this->container->getParameter('kernel.root_dir'));
    $parsed = Yaml::parse(file_get_contents($file));
    $options = $parsed['security']['firewalls']['main']['remember_me'];

    $endUserProvider = $this->container->get('AppBundle\Security\User\EndUserProvider');
    $secret = $providerKey = $this->container->getParameter('secret');
    $service = new TokenBasedRememberMeServices(array($endUserProvider), $secret, $providerKey, $options, null);
    $r = new \ReflectionMethod($service, 'onLoginSuccess');
    $r->setAccessible(true);
    $r->invoke($service, $request, $response, $token);

这里有一些解释.上面的代码首先是要记住我对security.yml的配置,这是启动TokenBasedRememberMeServices实例所必需的.然后使用ReflectionMethod克服可访问的问题,因为方法onLoginSuccess最初是受保护的.

Some explanation here. Above code is firstly fetch remember me configuration for security.yml which is needed to initiate TokenBasedRememberMeServices instance. Then use the ReflectionMethod to overcome the accessible issue because method onLoginSuccess is originally protected.

此解决方案在symfony 3.3.16下进行了测试.希望这对其他人有帮助.

This solution is tested under symfony 3.3.16. Hope this help others.

这篇关于symfony 3.3以“记住我"功能以编程方式登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆