如何在 Silex 中对密码进行编码?

How to encode password in Silex?

我正在使用 Silex 编写登录应用程序,但我遇到了 Silex 密码编码器的问题。我在 silex 文档中阅读了这个并得到了一些这样的代码:

// find the encoder for a UserInterface instance
$encoder = $app['security.encoder_factory']->getEncoder($user);

// compute the encoded password for foo
$password = $encoder->encodePassword('foo', $user->getSalt());

但是当我第一次访问我的网站时,我没有 $user 变量。我在哪里可以获得 $user 变量来编码我的密码?

更新我的解决方案

终于找到了解决办法。这是我获取编码密码的代码:

$encoded = $app['security.default_encoder']->encodePassword($string, '')

最好通过给自己一个答案来回答你的问题。这使您能够将其标记为已完成。其他用户将不必回头查看您的问题来为您提供解决方案。

对于这种情况,您可以将我的回答设置为完成,这样问题就关闭了。

使用此代码加密您的字符串:

$encoded = $app['security.default_encoder']->encodePassword($string, '')

您还可以使用其他方式在 Silex 中加密您的密码。它们可以在 here.

上找到

以下代码可用于select另一种加密:

$app['security.default_encoder'] = function ($app) {
    return $app['security.encoder.pbkdf2'];
};