2013-10-21 16 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(); 
} 
相关问题