Symfony 2.7记住我从来没有读过cookie

时间:2015-11-22 19:13:24

标签: symfony remember-me symfony-2.7

我使用Symfony 2.7.6,我已经配置了“记住我”#39;有关具有数据库后端的基于表单的身份验证系统的文档中描述的选项。一切都很好,但记住了我的选择。

我通过/ login页面登录并检查“记住我”,创建了两个cookie,PHPSID和REMEMBERME。

我关闭浏览器,再次打开页面并创建一个新的PHP cookie,REMEMBERME仍在那里。此时我尚未经过身份验证,请查看分析器日志,并且不存在查找/读取cookie的情况。

要开始使用安全性,请查看文档:

http://symfony.com/doc/current/book/security.html

security:

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    db_provider:
        entity:
            class: AppBundle:User
            property: email

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        form_login:
            remember_me: true
            login_path: /login
            check_path: /login_check
            default_target_path: homepage
        logout:
            path:   /logout
            target: /
        remember_me:
            key: "amsys_8222013"
            lifetime: 604800 # 1 week in seconds
            path: ~
            domain: ~
            always_remember_me: true

        provider: db_provider

access_control:
    # require ROLE_ADMIN for /admin*
    - {path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - {path: ^/, roles: ROLE_USER }
    - {path: ^/admin, roles: ROLE_USER }

encoders:
    AppBundle\Entity\User:
        algorithm: bcrypt

SecurityController.php

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class SecurityController extends Controller
{
/**
 * @Route("/login", name="login_route")
 */
public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render(
        'security/login.html.twig',
        array(
            // last username entered by the user
            'last_username' => $lastUsername,
            'error'         => $error,
        )
    );
}

/**
 * @Route("/login_check", name="login_check")
 */
public function loginCheckAction()
{
    // this controller will not be executed,
    // as the route is handled by the Security system
}
}

我现在已经被困了几个小时,我不知道问题出在哪里。

0 个答案:

没有答案