Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony检查用户是否经过身份验证_Php_Symfony - Fatal编程技术网

Php Symfony检查用户是否经过身份验证

Php Symfony检查用户是否经过身份验证,php,symfony,Php,Symfony,在我的Symfony项目中,我在我的User实体中使用UserInterface来处理身份验证。我还使用equalableinterface检查用户登录时的电子邮件是否已更改 public function isEqualTo(UserInterface $user) { if (!$user instanceof Account) { return false; } if ($this->email !== $user->getEmail()

在我的Symfony项目中,我在我的
User
实体中使用
UserInterface
来处理身份验证。我还使用
equalableinterface
检查用户登录时的电子邮件是否已更改

public function isEqualTo(UserInterface $user)
{
    if (!$user instanceof Account) {
        return false;
    }
    if ($this->email !== $user->getEmail()) {
        return false;
    }
    return true;
}
所有的工作如预期,但当我在DB中更改用户的电子邮件时,我并没有注销,只是没有验证,正如您在下面的屏幕截图中看到的那样


所以,如果用户经过身份验证,我知道如何签入控制器?当isEqualTo返回false时,我如何强制用户注销?

快速方法:请参阅
Symfony\Bundle\FrameworkBundle\Controller
中的方法
getUser
。如果您在服务中的某个位置需要此行为,请使用
security.token\u存储
服务作为依赖项


Try-way方法(类似symfony):您需要使用symfony-Security

快速方法:请参阅
symfony\Bundle\FrameworkBundle\Controller
中的方法
getUser
。如果您在服务中的某个位置需要此行为,请使用
security.token\u存储
服务作为依赖项


Try way method(symfony-like):您需要使用symfony-Security

我找到了解决方案,如果其他人有同样的问题,我想与大家分享

为了检查用户是否经过身份验证,我们需要一个由实现的。然后我们只需要调用方法


我找到了解决方案,如果其他人也有同样的问题,我想与大家分享

为了检查用户是否经过身份验证,我们需要一个由实现的。然后我们只需要调用方法

$tokenInterface = $this->get('security.token_storage')->getToken();
$isAuthenticated = $tokenInterface->isAuthenticated()