2013-05-28 58 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

相關問題