使用Symfony登录表单身份验证器进行身份验证时出错

时间:2019-09-19 08:20:21

标签: php symfony authentication

我的身份验证系统正在正常运行(我遵循了文档教程https://symfony.com/doc/current/security/form_login_setup.html)。

但是我有一个问题:当恶意用户禁用必需的密码字段并尝试登录时,我收到错误500“警告:空密码”

在一种情况下,身份验证系统不起作用:

当用户名与数据库中存储的用户名匹配但密码为空并且我尝试登录时,会出现500 Twig错误。

Symfony告诉我错误来自:UserPasswordEncoder->isPasswordValid(object(User), '') in src/Security/LoginFormAuthenticator.php (line 79)

这与以下代码相对应:

public function checkCredentials($credentials, UserInterface $user)    
{
    return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}

如果缺少密码之类的字段,我希望用户将重定向到登录页面。

谢谢。

1 个答案:

答案 0 :(得分:1)

我终于找到了解决方法:

public function checkCredentials($credentials, UserInterface $user)    
{
    if (empty($credentials['password'] {
        throw new CustomUserMessageAuthenticationException('Invalid password.');
    }

    return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}