douwei8672 2016-11-17 22:52
浏览 96
已采纳

使用Bcrypt的Symfony 3自定义用户提供程序

In reading https://symfony.com/doc/current/security/custom_provider.html#create-a-user-class/, all of the examples that I've seen implement the Symfony\Component\Security\Core\User\UserInterface for the User class. This interface defines a method for a salt field - but I'm wanting to use Bcrypt for the hashing algorithm.

In my app/config/security.yml file I have:

encoders:
    AppBundle\Security\User\WebserviceUser:
        algorithm: bcrypt
        cost: 12

The linked document says:

If getSalt() returns nothing, then the submitted password is simply encoded using the algorithm you specify in security.yml. If a salt is specified, then the following value is created and then hashed ...

Does this imply that if I specify for Bcrypt to be used, then I don't need a salt field in the users DB table (since the salt is in the same string as the rest of the password when hashed with Bcrypt)?

If that is the case then I'm guessing that I could just leave the getSalt() method with an empty body so that no salt would be specified and the algorithm in security.yml would be used.

Are my above assumptions correct? If they are not, how can I implement a user provider with bcrypt being used to hash passwords?

I'm using Symfony 3.1.6

  • 写回答

1条回答 默认 最新

  • doulachan8217 2016-11-17 23:06
    关注

    As stated in Creating your First User:

    Do you need to use a Salt property?

    If you use bcrypt, no. Otherwise, yes. All passwords must be hashed with a salt, but bcrypt does this internally. Since this tutorial does use bcrypt, the getSalt() method in User can just return null (it's not used). If you use a different algorithm, you'll need to uncomment the salt lines in the User entity and add a persisted salt property.

    if you want to use Bcrypt just return null in the getSalt() method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀