Rex*_*Rex 3 security encoder sha256 symfony
我最近开始为我的Symfony2项目设置安全性.我选择使用盐对sha256进行编码.当我尝试使用数据库中的示例帐户登录时(使用自我计算的sha256 salt/hash),它在没有给我任何错误消息的情况下仍然失败,我无法弄清楚原因.我决定在Controller的loginAction()方法中添加一些简单的代码.当用户无法使用指定的表单登录时,这是Symfony2调用的方法.我输入了以下代码:
$factory = $this->get('security.encoder_factory');
$em = $this->container->get('doctrine')->getEntityManager();
$userRep = $em->getRepository('MyProjectMyBundle:Users');
$user = $userRep->find(2);
$encoder = $factory->getEncoder($user);
$password = $encoder->encodePassword('cookie', 'thisisasalt');
$user->setPassword($password);
print($password);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试登录时,Symfony2给了我以下错误:
Catchable Fatal Error: Argument 1 passed to Symfony\Component\Security\Core\Encoder\EncoderFactory::getEncoder() must be an instance of Symfony\Component\Security\Core\User\UserInterface, instance of MyProject\MyBundle\Entity\Users given, called in /var/www/Symfony/src/MyProject/MyBundle/Controller/MainController.php on line 35 and defined in /var/www/Symfony/vendor/symfony/src/Symfony/Component/Security/Core/Encoder/EncoderFactory.php line 33
Run Code Online (Sandbox Code Playgroud)
所以基本上,它说的是getEncoder()必须是一个实例的论证Symfony\Component\Security\Core\User\UserInterface.但是,当我检查MyProject\MyBundle\Entity\Users.php时,它从以下行开始:
<?php
namespace MyProject\MyBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
...
Run Code Online (Sandbox Code Playgroud)
所以Users类实际上实现了UserInterface类.它包含UserInterface类中的所有函数.我已经按照Symfony2教程告诉我的方式创建了所有这些文件.Symfony2无法将我的Users实例识别为UserInterface实例的原因是什么?
PS:数据库是由其他人创建的,我只需要使用它.Users表包含的信息不仅仅是UserInterface所需的信息.
Rex*_*Rex 14
没关系,我是个白痴.
我忘了,除了包含UserInterface类之外,还必须确保您的类实现了UserInterface.
我改成了这个:
class Users implements UserInterface
Run Code Online (Sandbox Code Playgroud)
它现在完美无缺.
| 归档时间: |
|
| 查看次数: |
5256 次 |
| 最近记录: |