簡體   English   中英

使用symfony2通過URL自動登錄

[英]Auto-Login via URL with symfony2

我正在嘗試使用symfony2和一種特殊的網址進行自動登錄。 就像這里描述的那樣。

但是當我使用symfony2調試工具欄時,我注意到它說:“未經過身份驗證”。 但我有一個會話,我有一個用戶對象,這一切似乎工作得很好。 為什么調試工具欄會這樣說?

並且zadbuchy描述的方法有問題嗎? 我正在使用symfony 2.1.6。

編輯:我知道這可能不是“最安全”的登錄方式(感謝@Bart的討論),但我很好奇為什么symfony2無法正確識別登錄。

我的代碼看起來像這樣:

$firewall = "support_secured_area";
$token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
$this->get('security.context')->setToken($token);
$session = $this->get('session');
$session->set('_security_'.$firewall, serialize($token));

// Fire the login event (Suggestion from the answer, but unfortunately it doesn't work :( ).
$event = new InteractiveLoginEvent($this->getRequest(), $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

您需要InteractiveLoginEvent才能讓用戶登錄。

// Here, "public" is the name of the firewall in your security.yml
$token = new UsernamePasswordToken($user, $user->getPassword(), "public", $user->getRoles());
$this->get("security.context")->setToken($token);

// Fire the login event
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
$token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
$this->get('security.context')->setToken($token);
$session = $this->get('session');
$session->set('_security_'.$firewall, serialize($token));

我在幾個項目中應用了上述解決方案。 一個工作,但另一個沒有。

最終的解決方案是在下面的行中使用security_context_name而不是firewall_name。 然后它適用於每個項目。

$session->set('_security_'.$security_context_name, serialize($token));

感謝我的一位同事為我指出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM