2014-11-14 42 views
0

我正在使用fosuserbundle与自定义身份验证提供程序和mongodb持久用户。 用户类有一个属性作为对另一个mongodb集合的引用的集合持久化,但是此字段和其他字段未在安全令牌中序列化。 在我的另一个项目中,用户作为普通的旧php对象被正确保存并从令牌中提取出来,所以我不明白这个问题是否是由于mongodb水合作用导致的。fosuserbundle和用户在安全令牌

回答

1

通常,在令牌中持久化需要序列化的用户信息。 fosuserbundle将序列化属性:在“序列化”方法中定义的

/** 
* Serializes the user. 
* 
* The serialized data have to contain the fields used by the equals method and the username. 
* 
* @return string 
*/ 
public function serialize() 
{ 
    return serialize(array(
     $this->password, 
     $this->salt, 
     $this->usernameCanonical, 
     $this->username, 
     $this->expired, 
     $this->locked, 
     $this->credentialsExpired, 
     $this->enabled, 
     $this->id, 
    )); 
} 

。如果你想序列化你需要在你的User类中实现的其他属性,那么serialize/unserialize方法。这不是一个好习惯,因为当你从令牌中检索用户时,通常他会刷新。你在UserProvider中实现了“refreshToken”方法吗?

+0

事实上,在我的refreshUser方法中,我从旧用户获取数据并将其传递给nee实例,但您肯定是对的,我忽略了原始fos用户类中的serialize方法。 – 2014-11-16 18:05:34