Symfony登录序列化/取消序列化不起作用,无效字符串



我正在尝试使用Symfony文档中描述的serialize/userialize方法来登录用户。

当我在ContextListener中转储令牌并尝试取消字符串的序列化时,我会得到一个无效字符串的错误。

用户名字段实际上就是电子邮件字段。

用户实体

/**
 * @ORMTable(name="users")
 * @ORMEntity(repositoryClass="XXXUserBundleRepositoryUserRepository")
 * @ORMHasLifecycleCallbacks()
 * @UniqueEntity("username")
 */
class User extends AbstractEntity implements UserInterface, AdvancedUserInterface, Serializable
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORMManyToMany(
     *      targetEntity="XXXUserBundleEntityUserRoles",
     *     inversedBy="users"
     * )
     * @ORMJoinTable(
     *     name="users_user_role",
     *     joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORMJoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $roles;
    /**
     * @ORMColumn(name="password", type="string", length=64)
     */
    protected $password;
    /**
     * @AssertNotBlank()
     * @AssertLength(max=4096)
     */
    protected $plainPassword;
    /**
     * @ORMColumn(name="enabled", type="boolean")
     */
    protected $enabled = true;
    /**
     * @ORMColumn(name="locked", type="boolean")
     */
    protected $locked = false;
    /**
     * @ORMColumn(name="expired", type="boolean")
     */
    protected $expired = false;
    /**
     * @ORMColumn(name="expires_at", type="datetime", nullable=true)
     */
    protected $expiresAt;
    /**
     * @ORMColumn(name="last_login", type="datetime", nullable=true)
     */
    protected $lastLogin;
    /**
     * @ORMColumn(name="confirmation_token", type="string")
     */
    protected $confirmationToken;
    /**
     * @ORMColumn(name="password_requested_at", type="datetime", nullable=true)
     */
    protected $passwordRequestedAt;
    /**
     * @ORMColumn(name="credentials_expired", type="boolean")
     */
    protected $credentialsExpired = false;
    /**
     * @ORMColumn(name="user_default_language", type="string")
     */
    protected $userDefaultLanguage;
//getters and setters
public function isAccountNonExpired()
{
    return $this->getExpired();
}
public function isAccountNonLocked()
{
    return $this->getLocked();
}
public function isCredentialsNonExpired()
{
    return;
}
public function isEnabled()
{
    return $this->getEnabled();
}
/**
 * @return string
 */
public function serialize()
{
    return serialize([
        $this->id,
        $this->username,
        $this->password,
        $this->enabled,
    ]);
}
/**
 * @param string $serialized
 */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password,
        $this->enabled
        ) = $this->unserialize($serialized);
}

当我尝试登录时,我不会得到任何错误,只是一个ERR_EMPTY_RESPONSE。但是,当我从ContextListener::handle()方法中获得令牌并尝试取消序列化时,它表明字符串是错误的。

所以这与序列化有关,但我不知道/不明白我做错了什么。。。

您的问题中没有足够的信息,但我想这是多字节字符串的问题。在nginx/apache-logs文件夹或app/logs文件夹中检查日志。或者,您可以尝试在serialize/unserialize方法中使用json_encode/decode函数,而不是serialize/unserialize函数。例如:

public function serialize()
{
    return json_encode([$this->id]);
}
public function unserialize($serialized)
{
    list($this->id) = json_decode($serialized);
}

最新更新