Symfony authentication token getCredentials returning null

主宰稳场 提交于 2019-12-11 04:49:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!