2013-10-21 15 views
0

我有一個針對數據庫的用戶authenticaton應用程序。我使用的財產是電子郵件:Symfony2:「記住我」嘗試通過電子郵件的用戶名instad進行身份驗證

providers: 
     administrators: 
      entity: 
       class: CorabMainBundle:User 
       property: email 

身份驗證很好! 但我有記憶功能工作的巨大問題。 幾個小時後,我想我找到了問題,但我不知道如何解決它...

Symfony2似乎嘗試使用用戶名而不是電子郵件進行身份驗證,以防萬一記住我。

dev.log說以下內容:

[2013-10-21 23:49:19] security.DEBUG: Remember-me cookie detected. [] [] 
[2013-10-21 23:49:19] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.salt AS salt3, t0.password AS password4, t0.email AS email5, t0.is_active AS is_active6, t0.organisation AS organisation7 FROM User t0 WHERE t0.email = ? LIMIT 1 ["roger"] [] 
[2013-10-21 23:49:19] security.INFO: User for remember-me cookie not found. [] [] 
[2013-10-21 23:49:19] security.DEBUG: Clearing remember-me cookie "REMEMBERME" [] [] 

2號線是不正常怎麼一回事,因爲它不應該是羅傑而是電子郵件地址。這就是爲什麼cookie被刪除。

如何告訴symfony2使用電子郵件作爲屬性記住我的身份驗證?

謝謝你的幫助!

回答

5

您可以擴展默認記住我服務類並覆蓋onLoginSuccess方法,以便它使用電子郵件而不是用戶名。

  • 服務擴展:security.authentication.rememberme.services.simplehash.class
  • 類:的Symfony \分量\安全\ HTTP \與rememberMe \ TokenBasedRememberMeServices
  • 方法:onLoginSuccess
2

或者您可以在getUsername()函數上返回電子郵件,並更改真實用戶名字段的名稱,如「綽號」 ..

public function getUsername() 
{ 
    return $this->email; 
} 

當你實現了的UserInterface,在UserInterface.php評論說

/** 
* Returns the username used to authenticate the user. 
* 
* @return string The username 
*/ 
public function getUsername(); 

是的,它是 「壞」,但更簡潔。真正的問題是這個該死的功能名稱。 Symfony2的應該改變它..

0

在我來說,我剛纔修改「與rememberMe」 cookie,並通過電子郵件這樣的更換用戶名:

if(userJustLogin) { 
    // decode cookie 
    $request = $this->getRequest(); 
    $cookies = $request->cookies->all(); 
    $cookieParts = explode(':', base64_decode($cookies["REMEMBERME"])); 

    // replace username by email and update hash 
    list($class, $username, $expires, $hash) = $cookieParts; 
    $cookieParts[1] = base64_encode($user->getMail()); 
    $cookieParts[3] = hash_hmac('sha256', $class.$user->getMail().$expires.$user->getPassword(), 'YourSecretTokenFromParameters.yml'); 

    // reencode cookie 
    $cookies["REMEMBERME"] = base64_encode(implode(':', $cookieParts)); 
    $cookie = new Cookie('REMEMBERME', $cookies["REMEMBERME"], $expires); 

    // send new cookie 
    $response = new Response(); 
    $response->headers->setCookie($cookie); 
    $response->send(); 
} 
相關問題