1

I am trying to implement a login page with symfony2 and for that I am using my own custom user provider.

The problem is when the user enters his credentials, he will not be recognized. By that, I mean the debug bar at the bottom says "You are not authenticated." and $request->getUser() will return null. But the strange thing is that the user will still be allowed to visit the pages that need him to log in.

I don't think the problem is with the authentication, since when I enter a wrong password, I get warned about it, but when I enter the correct one, I get redirected to the first page (but it still says "You are not authenticated.")

Do you know where I should be looking for the problem?

I have attached my security.yml file in this pastebin and routing.yml in this one.
Here is the code for my custom user provider.
And This is the User class definition.

EDIT: Here is the var_dump of my get('security.context')->getToken(). The funny thing is that authenticated is true, but getUser() is still null and the debug bar says I am not authenticated.

object(Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken)#46 (6) {
  ["credentials":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
  NULL
  ["providerKey":"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":private]=>
  string(11) "system_area"
  ["user":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  NULL
  ["roles":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  array(2) {
    [0]=>
    object(Symfony\Component\Security\Core\Role\Role)#45 (1) {
      ["role":"Symfony\Component\Security\Core\Role\Role":private]=>
      string(10) "ROLE_ADMIN"
    }
    [1]=>
    object(Symfony\Component\Security\Core\Role\Role)#44 (1) {
      ["role":"Symfony\Component\Security\Core\Role\Role":private]=>
      string(9) "ROLE_USER"
    }
  }
  ["authenticated":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  bool(true)
  ["attributes":"Symfony\Component\Security\Core\Authentication\Token\AbstractToken":private]=>
  array(0) {
  }
}
4

2 回答 2

2

为了获得当前用户,您必须使用:

$this->get('security.context')->getToken()->getUser();

为了检查当前用户是否具有特定角色,请使用:

 $this->get('security.context')->isGranted('ROLE_USER')
于 2013-05-28T15:42:21.650 回答
1

我设法解决了这个问题。这是因为我使用虚拟代码代替了用户对象的序列化。我用真实的代码替换了它,问题就消失了。

于 2013-05-28T16:56:42.043 回答