在symfony身份验证中反序列化用户数据时出错

时间:2014-09-19 03:49:46

标签: security symfony serialization offset

我的第一个问题。我一直坐在上面几个小时,找不到解决办法: 当用户在数据库中成功找到并且我猜symfony尝试将其数据序列化为会话时,会弹出错误。

Notice: unserialize(): Error at offset 37 of 49 bytes in G:\cebuland\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Token\AbstractToken.php line 163

导致登录过程失败。

用户实体:

/**
 * @ORM\Entity
 * @ORM\Table(name="User")
 */
class User implements AdvancedUserInterface, \Serializable{
...
    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="user")
     * @var $role Doctrine\Common\Collections\ArrayCollection
     */
    private $role;
...
    public function getRoles() {
        return $this->role->toArray();
    }
...
    public function serialize() {
        serialize(array(
            $this->id, 
            $this->name,
            $this->password,
            $this->created,
            $this->last_activity,
            $this->ghost,
            $this->role
        ));
    }
    public function unserialize($serialized) {
        list(
            $this->id, 
            $this->name,
            $this->password,
            $this->created,
            $this->last_activity,
            $this->ghost,
            $this->role
        ) = unserialize($serialized);
    }

角色的实体非常相似。

security.yml配置文件:

security:
    providers:
        users:
            entity: {class: ApplicationMainBundle:User, property: name}
    encoders: 
        Application\MainBundle\Entity\User:
            #plain just for testing
            algorithm: plaintext 
    firewalls:
        secured_area:
            logout: 
                path: /logout
            pattern:   ^/
            anonymous: ~
            form_login:
                login_path: /login
                check_path: /login_check
    access_control:
        - { path: ^/admin/, roles: ROLE_ADMINISTRATOR}

无法在互联网上的任何地方找到线索。

1 个答案:

答案 0 :(得分:0)

您的User :: serialize方法不返回序列化字符串,但应该Serializable::serialize