简体   繁体   English

防止用户在Symfony中使用相同的密码(bcrypt)

[英]Prevent user from using same password in Symfony (bcrypt)

I would like to prevent users from using the same password they used in the last 3 months. 我想阻止用户使用他们在过去3个月内使用的相同密码。

My first approach was to create a table to store a password history for each user, the problem is Symfony uses Bcrypt to encode passwords, and it hashes it differently each time, so my idea of simply comparing strings won't work. 我的第一种方法是创建一个表来存储每个用户的密码历史记录,问题是Symfony使用Bcrypt对密码进行编码,并且每次都以不同方式对其进行哈希处理,所以我简单地比较字符串的想法是行不通的。

Is there a way to execute IsPasswordValid against an entity that is not implementing UserInterface? 有没有办法对未实现UserInterface的实体执行IsPasswordValid? So I could check if new password returns true for each stored password... 所以我可以检查每个存储密码的新密码是否返回true ...

Also any other ideas are welcome. 也欢迎任何其他想法。

I'm using Symfony 3.0.6, and I'm not willing to use FOS_User_Bundle, I already know how to make it work with it. 我正在使用Symfony 3.0.6,我不愿意使用FOS_User_Bundle,我已经知道如何使用它。

Thanks. 谢谢。

You'll want to use password_verify() to see if their new password that they have entered matches against your past x passwords that you have stored. 您需要使用password_verify()来查看他们输入的新密码是否与您存储的过去的x密码相匹配。

So a very simple example; 这是一个非常简单的例子;

$oldPassword = password_hash('password', PASSWORD_DEFAULT);
$newPassword = 'password';

if (password_verify($newPassword, $oldPassword)) {
    echo 'Previously used password, please choose another';
}

Essentially you just need to pull out the user's old passwords, and loop through the data and then use password_verify against each of the old ones to see if any match -- if they do then you know they've used a previously stored password. 基本上你只需要提取用户的旧密码,然后循环访问数据,然后对每个旧password_verify使用password_verify来查看是否有任何匹配 - 如果他们这样做,那么你知道他们已经使用了以前存储的密码。

As a little side note, if you wanted to go one step further and do similarity checks, you would have to generate all the permutations yourself and then follow the same routine of using password verify on the old passwords against all your permutations. 作为一个小小的注意事项,如果你想更进一步并进行相似性检查,你必须自己生成所有的排列,然后按照旧密码使用密码验证的相同例程来对抗所有排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM