简体   繁体   English

Symfony2:记住我令牌未设置

[英]Symfony2: remember me token is not set

I did everything as needed, yet my 'remember me' token is not set. 我已根据需要进行了所有操作,但未设置“记住我”令牌。 I tracked the code until the part in /vendor/symfony/src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeService.php where the cookie is set 我一直跟踪代码,直到/vendor/symfony/src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeService.php中设置了Cookie的部分为止

$response->headers->setCookie(
        new Cookie(
            $this->options['name'],
            $value,
            $expires,
            $this->options['path'],
            $this->options['domain'],
            $this->options['secure'],
            $this->options['httponly']
        )
    );

When I do a dump of $response->headers, the cookies part looks like this: 当我转储$ response-> headers时,cookie部分看起来像这样:

["cookies":protected]=> array(1) { [""]=> array(1) { ["/"]=> array(1) { ["REMEMBERME"]=> object(Symfony\Component\HttpFoundation\Cookie)#753 (7) { ["name":protected]=> string(10) "REMEMBERME" ["value":protected]=> string(176) "V2Vic3BpblxVc2VyQnVuZGxlXEVudGl0eVxVc2VyOmJXRmliMmRwWlVCbmJXRnBiQzVqYjIwPToxMzM5MjQ5Mjc5OmJjY2QxMWYxNGNkZmQxZmI5ZTNjOTBhYTBiMTEyNjEwYzdkMWYxOGYwYWQzMmMzYmJhYzZlODM3Yjc0Nzc3Mjk=" ["domain":protected]=> NULL ["expire":protected]=> int(1339249279) ["path":protected]=> string(1) "/" ["secure":protected]=> bool(false) ["httpOnly":protected]=> bool(true) } } } } 

The cookies array first element's key is empty. Cookies数组第一个元素的键为空。 Is that correct? 那是对的吗?

And if that's correct, why is the cookie not set? 如果是正确的话,为什么没有设置cookie?

edit: My security.yml: 编辑:我的security.yml:

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            login_path: fos_user_security_login 
            check_path: fos_user_security_check 
        logout:
            path:        _security_logout
            target:      _welcome
        anonymous:    true
        remember_me:
            key:      "%secret%"
            lifetime: 36000
            path:     /
            domain:   ~

If you need any more information, let me know. 如果您需要更多信息,请告诉我。

Ok I got this fixed. 好的,我已经解决了。

The reason had to do with my interactiveloginlistener. 原因与我的交互式登录侦听器有关。 I listened to succesful logins, yet before the headers were sent I redirected with a new RedirectResponse, which of course didn't include the cookies. 我听了成功的登录信息,但是在发送标头之前,我使用了一个新的RedirectResponse进行了重定向,该重定向当然不包括cookie。

Phew. 唷。

Just in case anyone else is also struggling with figuring out why the cookie was not being set, here's what the problem was for me. 万一其他人也在努力弄清楚为什么未设置Cookie的问题,这就是我的问题所在。

I added the _remember_me checkbox to my form via the builder in my LoginFormType , which meant the field's name when rendered was actually login_form[_remember_me] , not _remember_me as expected. 我通过LoginFormType的构建器在表单中添加了_remember_me复选框,这意味着呈现时字段的名称实际上是login_form[_remember_me]而不是预期的_remember_me

This meant that the authentication system perceived the logins as not wanting to be remembered even when the box was checked, and hence no cookie was set. 这意味着即使选中该复选框,身份验证系统仍认为不想记住登录名,因此未设置cookie。 Setting the proper field name in security.yml fixed it. security.yml设置适当的字段名称可以修复该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM