ma-*_*ick 5 php autologin symfony symfony-2.1
我正在尝试使用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);
Run Code Online (Sandbox Code Playgroud)
您需要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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6094 次 |
| 最近记录: |