以编程方式注销当前用户 [英] Programmatically logout current user
问题描述
我正在尝试以编程方式从侦听器内部注销当前用户.我在此处阅读了
I am trying to programmatically logout the current user from inside a listener. I read here that
$this->get('security.context')->setToken(null);
$this->get('request')->getSession()->invalidate();
可以解决问题,但是我不能调用 $ this-> container-> get('security.context')-> getToken()-> getUser();
因为令牌现在是 NULL
.
does the trick but then I can't call $this->container->get('security.context')->getToken()->getUser();
anymore as the token is now NULL
.
如何注销用户,但仍然让应用程序正常运行?我在控制器函数中调用了getUser(),因此我应该将令牌重新设置为与未经身份验证的用户相对应的名称.我该怎么办?
How can I log out the user but still let the application run normally? I have calls to getUser() in my controller functions so I should set back the token to something corresponding to an non authenticated user. How can I do this?
此外,如果有一种方法可以通过编程方式开始一个新会话并设置一条Flash消息来通知用户他已经注销,那真是太棒了.
Also, if there is a way to programmatically start a new session and set a flash message to inform the user he has been logged out, it would be awesome.
推荐答案
尝试
$anonToken = new AnonymousToken('theTokensKey', 'anon.', array());
$this->get('security.context')->setToken($anonToken);
第一个参数是令牌的密钥(即'50cdf89882454')
first parameter is the token's key (i.e. '50cdf89882454')
这篇关于以编程方式注销当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!