3

我正在尝试为 Sulu CMF 前端进行用户注册。我想对密码进行哈希处理。但出于任何原因,我无法获得该服务。

You have requested a non-existent service "security.encoder_factory".

或者

You have requested a non-existent service "security.password_encoder"

如果我要求

app/console container:debug | grep encode

两种服务都在那里

$ app/console container:debug | grep encode                    
263: security.encoder_factory                                                      Symfony\Component\Security\Core\Encoder\EncoderFactory                                     
266: security.password_encoder                                                     Symfony\Component\Security\Core\Encoder\UserPasswordEncoder                                

你有什么建议给我吗?我的控制器知道容器,我的代码如下:

$this->container->get('security.password_encoder')

下面是 conainer:debug 的一些输出

$ app/console container:debug security.password_encoder
[container] Information for service security.password_encoder
Service Id       security.password_encoder
Class            Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
Tags             -
Scope            container
Public           yes
Synthetic        no
Lazy             no
Synchronized     no
Abstract         no


$ app/console container:debug security.encoder_factory 
[container] Information for service security.encoder_factory
Service Id       security.encoder_factory
Class            Symfony\Component\Security\Core\Encoder\EncoderFactory
Tags             -
Scope            container
Public           yes
Synthetic        no
Lazy             no
Synchronized     no
Abstract         no

我目前安装的 symfony 版本是 v2.6.12

sulu 提供的命令使用了这种方法。它适用于开发和生产两种环境。

/**
 * Encodes the given password, for the given password, with he given salt and returns the result.
 *
 * @param $user
 * @param $password
 * @param $salt
 *
 * @return mixed
 */
private function encodePassword($user, $password, $salt)
{
    /** @var PasswordEncoderInterface $encoder */
    $encoder = $this->getContainer()->get('security.encoder_factory')->getEncoder($user);

    return $encoder->encodePassword($password, $salt);
}
4

2 回答 2

2

正如 xabbuh 在评论中已经指出的那样,我们在 Sulu 中有两个不同的内核。如果您使用该app/webconsole命令,则您正在使用网站的内核。

在标准安装中我们不包括SecurityBundle,因为它会增加一些开销,并且对于简单的网站来说不是必需的。但是,如果您正在构建更复杂的应用程序,您可能还希望保护您的网站。在这种情况下,您可以简单地将 的新实例添加Symfony\Bundle\SecurityBundle\SecurityBundle到. 之后也将提供您一直要求的服务。$bundlesapp/WebsiteKernel

于 2016-01-13T12:21:03.307 回答
1

Sulu CMF 有一个名为 app/console webconsole 的额外控制台应用程序,它使用不同的内核 :-)

我搜索的服务在那里不存在。相反,我能够使用

$userManager = $this->container->get('sulu_security.user_manager');
于 2016-01-11T15:02:38.633 回答