问题描述
我有一个实现SimplePreAuthenticatorInterface
,AuthenticationSuccessHandlerInterface
和AuthenticationFailureHandlerInterface
的TokenAuthenticator
.它会创建一个PreAuthenticatedToken
令牌.
I have a TokenAuthenticator
which implements SimplePreAuthenticatorInterface
, AuthenticationSuccessHandlerInterface
and AuthenticationFailureHandlerInterface
. It creates a PreAuthenticatedToken
token.
在该类中,我有一个名为authenticateToken
的方法,它看起来像这样.
Within that class I have a method called authenticateToken
which looks like this.
/**
* @param TokenInterface $token
* @param UserProviderInterface $userProvider
* @param $providerKey
*
* @return PreAuthenticatedToken
*/
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$token = $token->getCredentials();
该代码有效,但是最近有几次getCredentials
返回null
导致代码掉落.
The code works, however there have been a couple of occasions recently where getCredentials
has returned null
causing the code to fall over.
我正在尝试确定原因,并考虑了用户使用专用浏览器会话和/或清除其会话cookie/缓存等,但是我似乎无法复制.
I am trying to ascertain why this is and have considered users using private browser sessions and/or clearing their session cookies/cache etc, but I cannot seem to replicate this.
考虑authenticateToken
方法的类型,将$token
变量提示为TokenInterface
-是什么导致对getCredentials
的调用然后返回null
?
Considering the authenticateToken
method type-hints the $token
variable to a TokenInterface
- what would cause a call to getCredentials
to then return null
?
推荐答案
是否要求对所有路由进行所有身份验证?
Are you requiring all authentication for all routes?
如果您允许匿名用户,则getCredentials
将返回''
,如您从该类的代码段中所见
If you allow anonymous users getCredentials
will return ''
, as you can see in this snippet from the class
/**
* {@inheritdoc}
*/
public function getCredentials()
{
return '';
}
另一个返回此默认令牌的默认令牌类是RememberMeToken
.
The other default token class that returns this is the RememberMeToken
.
这篇关于Symfony身份验证令牌getCredentials返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!