【发布时间】:2018-08-04 18:02:22
【问题描述】:
我正在尝试验证用户,我无法使用 FOSub,因为它与 Symfony 4 不兼容,我正在编写我的代码。 我通过 ajax 将用户名和密码传递给服务。尝试登录用户的方法:
public function login($username, $password) {
$user = $this->entityManager->getRepository("App:Users")
->findOneBy(array('username' => $username));
}
if(!$this->encoder->isPasswordValid($user->getPassword(), $password)) {
return new Response(
'Username or Password not valid.',
Response::HTTP_UNAUTHORIZED,
array('Content-type' => 'application/json')
);
}
}
当我调用此方法时出现错误:
类型错误:参数 1 传递给 Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::isPasswordValid() 必须实现接口 Symfony\Component\Security\Core\User\UserInterface,给定字符串,在 /src/Service 中调用/Login.php 第 60 行
为什么它告诉我在 Symfony 文档中有一些服务时我必须实现一些服务
isPasswordValid(string $encoded, string $raw, string $salt),根据编码密码检查原始密码。
我不明白这个...
【问题讨论】:
-
我认为您正在查看错误的文档,PasswordEncoderInterface 有您提到的定义,但您应该查看 UserPasswordEncoder。你的 User 类需要实现UserInterface。
-
@JimL 我的实体类用户已经实现了 UserInterface:类用户实现了 UserInterface
-
太棒了!但正如您从文档中看到的那样,我链接到
UserPasswordEncoder::isPasswordValid需要两个参数,UserInterface $user and string $plainPassword。不是您发送的$hash, $plainPassword -
只需发送
isPasswordValid($user, $password)就完成了,@JimL 是对的,您正在查看接口而不是实现 -
确实如此,那么我该如何创建这个 UserInterface $user 对象呢?