【问题标题】:Symfony 3: RememberMe Cookie is not setSymfony 3:RememberMe Cookie 未设置
【发布时间】:2018-08-02 09:00:48
【问题描述】:

在我的 Symfony 3 应用程序中,我的登录页面位于根 URL“/”(因此不是“/login”。

很遗憾,尽管在 security.yml 中正确配置了 REMEMBER_ME cookie,但该应用并未设置:

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
            fos_userbundle:
                id: fos_user.user_provider.username_email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt|error)|css|images|js)/
            security: false
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                check_path: /login_check
                login_path: /
                default_target_path:  /home
                use_forward:    false
                failure_path:   null
                failure_handler: ccdn_user_security.component.authentication.handler.login_failure_handler
                require_previous_session: false
            logout:
                path:   /logout
                target: /
            security: true
            anonymous:
                secret:  "%secret%"
            remember_me:
                secret: "%secret%"
                lifetime: 604800 # 1 week in seconds
                path:     /
                secure: true
            switch_user: true

    access_control:
        - { path: ^/admin, role: ROLE_ADMIN,requires_channel: "%protocol%" }
        - { path: ^/user, roles: ROLE_USER, requires_channel: "%protocol%"}
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: "%protocol%" }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: "%protocol%" }
        - { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: "%protocol%" }

我没有任何类型的侦听器设置,这是该 OP 遇到的问题: Symfony2: remember me token is not set

我已将 FOSUserBundle 的 SecurityController 调整如下:

/**
 * Controllers for Anonymous Index Page
 */
class SecurityController extends BaseController
{
    /**
     * @param Request $request
     *
     * @return Response
     */
    public function loginAction(Request $request)
    {
        $securityContext = $this->container->get('security.authorization_checker');

        if ( $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') or $securityContext->isGranted('IS_AUTHENTICATED_FULLY') ) {
            return $this->redirect($this->generateUrl('home'));
        }

        $response = parent::loginAction($request);
        return $response;

    }
}

但是正如你所看到的,这只是在用户已经登录的情况下重定向用户。

但是,FOSUserBundle 附带的 AuthenticationListener 似乎从未被触发。

最后,如果你需要它,这是我在登录表单中的记住我小部件:

   <div class="checkbox checkbox-css m-b-30">
          <input name="_remember_me" checked type="checkbox" id="remember_me_checkbox" />
          <label for="remember_me_checkbox">Onthoudt mij</label>
   </div>

有人知道为什么没有设置 cookie 吗? 用户在 20 分钟左右后自动注销。我猜这是因为 PHP 会话到期?

【问题讨论】:

  • 您的服务器(开发服务器?)是否设置为使用 SSL/TLS?如果是这样,您是否通过https 访问该站点? remember_me 下的 secure: true 引爆 cookie 只会通过安全连接发送...
  • 另外,我相信“用户将在 20 分钟左右后自动注销”不会通过修复此问题而消失。您应该改为增加会话超时...
  • @JovanPerovic 删除安全后:真的它再次工作(即使我正在运行 https)。谢谢!。会话超时我会观察到,但我认为这将解决它。

标签: symfony fosuserbundle


【解决方案1】:

根据评论区回答:

secure: true 表示 cookie 将仅通过安全连接发送。出于测试目的,您可能希望删除此行,或者检查您的 Web 服务器是否已正确配置为处理 https 流量。

确保您通过https 访问您的应用。我不确定自签名证书是否可能对此产生任何影响。根据 https://stackoverflow.com/a/9606871/662615 的 Gumbo 的回答,它不应该...

另一件事:为了防止过早注销,请改为增加会话超时。 AFAIK,remember_me 仅在用户在会话期间离开时才有帮助...

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多