2013-05-28 50 views
1

我正在尝试使用symfony2实现登录页面,并且我正在使用自己的自定义用户提供程序。symfony不承认已通过身份验证的用户

问题是当用户输入他的凭证时,他不会被识别。那么,我的意思是底部的调试栏显示“您没有通过身份验证”。而$request->getUser()将返回null。但奇怪的是,用户仍然被允许访问需要他登录的页面。

我认为问题不在于身份验证,因为当我输入错误的密码时,会收到警告关于它,但是当我输入正确的一个,我被重定向到第一页(但它仍然说“你没有通过身份验证”。)

你知道我应该在哪里寻找问题吗?

我已将security.yml文件附加在this pastebinrouting.ymlthis one中。
Here是我的自定义用户提供程序的代码。
This是用户类定义。

编辑:这是我的get('security.context')->getToken()的var_dump。有趣的是authenticated是真的,但getUser()仍然是空的,调试栏说我没有通过身份验证。

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) { 
    } 
} 

回答

1

我设法解决问题。这是因为我使用了伪代码来代替User对象的序列化。我用真实的代码取代了它,问题就没有了。

2

为了让你有使用当前用户:

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

为了检查当前用户具有一定的作用用途:

$this->get('security.context')->isGranted('ROLE_USER') 
+0

$ this-> get('security.context') - > getToken() - > getUser()为空 – Untitled

+0

您是否检查过您的会话已启动? $ this-> get('session') - > isStarted()?如果用户在加载页面时加载,请检查您的app/logs/dev.log。 – nifr

+0

是的。它开始了。 – Untitled

相关问题