2014-11-14 43 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