问题
I have a TokenAuthenticator which implements SimplePreAuthenticatorInterface, AuthenticationSuccessHandlerInterface and AuthenticationFailureHandlerInterface. It creates a PreAuthenticatedToken token.
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();
The code works, however there have been a couple of occasions recently where getCredentials has returned null causing the code to fall over.
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.
Considering the authenticateToken method type-hints the $token variable to a TokenInterface - what would cause a call to getCredentials to then return null?
回答1:
Are you requiring all authentication for all routes?
If you allow anonymous users getCredentials will return '', as you can see in this snippet from the class
/**
* {@inheritdoc}
*/
public function getCredentials()
{
return '';
}
The other default token class that returns this is the RememberMeToken.
来源:https://stackoverflow.com/questions/42337634/symfony-authentication-token-getcredentials-returning-null