从onAuthenticationFailure()中的会话中获取用户名

时间:2018-05-15 19:13:37

标签: php symfony

简而言之:在Custom Authentication System with Guard中,如何访问当前在会话中的用户名,而不是当前登录尝试中的用户名?

更长版本:
我使用包含令牌的URL来验证用户身份。如果在用户登录时令牌失效,然后用户再次打开相同的URL,则情况为:当前登录尝试失败(即onAuthenticationFailure()启动),因为令牌不是有效了。但是,用户仍然登录,因为会话中的数据从未更改过。在Twig中,{{ app.user }}为我提供了我正在寻找的内容(即会话中的用户名)。如何从onAuthenticationFailure()内访问它?

1 个答案:

答案 0 :(得分:0)

\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface注入您的身份验证器类(TokenAuthenticator.php )并在其上调用->getToken()->getUser()

如果用户之前未登录,->getToken()null