最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

关于php:Symfony2 / FOS用户软件包-注册后请记住我

XAMPP相关 admin 656浏览 0评论

Symfony2 / FOS user bundle – Remember me after registration

我正在尝试在用户注册时设置REMEMBERME cookie。我的用户注册后直接登录,没有确认电子邮件。

我不想使用always_remember_me功能,因为我仍然希望用户选择一个复选框。

我在FOS \\\\\\\\ UserBundle \\\\\\\\ Security \\\\\\\\ LoginManager

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
final public function loginUser($firewallName, UserInterface $user, Response $response = null)
{
$this->userChecker->checkPostAuth($user);

$token = $this->createToken($firewallName, $user);

if ($this->container->isScopeActive(‘request’)) {
$this->sessionStrategy->onAuthentication($this->container->get(‘request’), $token);

if (null !== $response) {
$rememberMeServices = null;
if ($this->container->has(‘security.authentication.rememberme.services.persistent.’.$firewallName)) {
$rememberMeServices = $this->container->get(‘security.authentication.rememberme.services.persistent.’.$firewallName);
} elseif ($this->container->has(‘security.authentication.rememberme.services.simplehash.’.$firewallName)) {
$rememberMeServices = $this->container->get(‘security.authentication.rememberme.services.simplehash.’.$firewallName);
}

if ($rememberMeServices instanceof RememberMeServicesInterface) {
$rememberMeServices->loginSuccess($this->container->get(‘request’), $response, $token);
}
}
}

$this->securityContext->setToken($token);
}

 

中发现了由FOS \\’RegistrationController:

调用的函数我服务并设置令牌。不幸的是,由于我没有任何东西而没有被触发。

如何创建”记住我”服务?有没有更直接的方法来在寄存器上设置REMEMBERME cookie?

谢谢。

相关讨论

看到这个答案stackoverflow.com/a/20550520/5397119,我认为您可以使用onRegistrationSuccess方法(请参阅FOSUserEvents.php)来扩展所需的功能。

这似乎是一个老错误,自2012年以来仍未修复。我发现的解决方案是在我自己的用户捆绑中实施此PR。

注意:此版本适用于1.3,尚未尝试2.0

  • 添加此编译器遍历:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace AppUserBundle\\DependencyInjection\\Compiler;

use Symfony\\Component\\DependencyInjection\\ContainerBuilder;
use Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface;
use Symfony\\Component\\DependencyInjection\
eference;

/**
 * Registers the additional validators according to the storage
 *
 * @author Vasily Khayrulin <sirianru@gmail.com>
 */

class InjectRememberMeServicesPass implements CompilerPassInterface
{
    /**
     * {@inheritDoc}
     */

    public function process(ContainerBuilder $container)
    {

        $rememberMeServices = array();
        foreach ($container->getDefinitions() as $id => $definition) {
            if (0 !== strpos($id, ‘security.authentication.rememberme.services.’)) {
                continue;
            }
            if ($definition->isAbstract()) {
                continue;
            }
            $firewallName = $definition->getArgument(2);
            $rememberMeServices[$firewallName] = new Reference($id);
        }

        $loginManager = $container->getDefinition(‘fos_user.security.login_manager’);
        $loginManager->replaceArgument(4, $rememberMeServices);
    }
}
  • 在捆绑包的版本中实例化它:覆盖LoginManager:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace AppUserBundle\\Security;

use FOS\\UserBundle\\Model\\UserInterface;
use FOS\\UserBundle\\Security\\LoginManagerInterface;
use Symfony\\Component\\DependencyInjection\\ContainerInterface;
use Symfony\\Component\\HttpFoundation\
esponse;
use Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken;
use Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface;
use Symfony\\Component\\Security\\Core\\SecurityContextInterface;
use Symfony\\Component\\Security\\Http\
ememberMe\\AbstractRememberMeServices;
use Symfony\\Component\\Security\\Http\
ememberMe\
ememberMeServicesInterface;
use Symfony\\Component\\Security\\Http\\Session\\SessionAuthenticationStrategyInterface;

/**
 * Abstracts process for manually logging in a user.
 *
 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 */

class LoginManager implements LoginManagerInterface
{
    private $securityContext;
    private $userChecker;
    private $sessionStrategy;
    private $container;

    /**
     * @var AbstractRememberMeServices[]
     */

    private $rememberMeServices;

    public function __construct(
        SecurityContextInterface $context,
        UserCheckerInterface $userChecker,
        SessionAuthenticationStrategyInterface $sessionStrategy,
        ContainerInterface $container,
        $rememberMeServices
    ) {
        $this->securityContext    = $context;
        $this->userChecker        = $userChecker;
        $this->sessionStrategy    = $sessionStrategy;
        $this->container          = $container;
        $this->rememberMeServices = $rememberMeServices;
    }

    final public function loginUser($firewallName, UserInterface $user, Response $response = null)
    {
        $this->userChecker->checkPostAuth($user);

        $token = $this->createToken($firewallName, $user);

        if ($this->container->isScopeActive(‘request’)) {
            $this->sessionStrategy->onAuthentication($this->container->get(‘request’), $token);

            if (null !== $response && isset( $this->rememberMeServices[$firewallName] )) {
                $rememberMeServices = $this->rememberMeServices[$firewallName];
                if ($rememberMeServices instanceof RememberMeServicesInterface) {
                    $rememberMeServices->loginSuccess($this->container->get(‘request’), $response, $token);
                }
            }
        }

        $this->securityContext->setToken($token);
    }

    protected function createToken($firewall, UserInterface $user)
    {
        return new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
    }
}
  • 也覆盖您的services.yml文件中的服务声明:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    services:
        # …
           fos_user.security.login_manager:
                class: AppUserBundle\\Security\\LoginManager
                arguments:
                     @security.context
                     @security.user_checker
                     @security.authentication.session_strategy
                     @service_container
                     { }
  • 最后,在register.html.twig模板上添加” remember_me”复选框:

    1
    2
    <input type=“checkbox” id=“remember_me” name=“_remember_me” checked />
    <label for=“remember_me”>Keep me logged in</label>

中发现了由FOS \\’RegistrationController:

调用的函数我服务并设置令牌。不幸的是,由于我没有任何东西而没有被触发。

如何创建”记住我”服务?有没有更直接的方法来在寄存器上设置REMEMBERME cookie?

谢谢。

  • 看到这个答案stackoverflow.com/a/20550520/5397119,我认为您可以使用onRegistrationSuccess方法(请参阅FOSUserEvents.php)来扩展所需的功能。


这似乎是一个老错误,自2012年以来仍未修复。我发现的解决方案是在我自己的用户捆绑中实施此PR。

转载请注明:XAMPP中文组官网 » 关于php:Symfony2 / FOS用户软件包-注册后请记住我

您必须 登录 才能发表评论!