如何安全地检查用户名是否已被占用?
我有一个可以创建新用户的表单。我想添加一些 AJAX 来检查用户名是否被占用,一旦输入字段失去焦点。 我不太确定如何解决这个问题,因为这可能为暴力攻击铺平道路,因为您可以检查任何用户名并查看它是否存在。 有什么建议吗?
I have a form where someone can create a new user. I'd like to add some AJAX to check if a username is taken, as soon as the input field loses focus.
I'm not really sure how to go about this, as this could pave the way to bruteforce attack, since you could check for any username and see whether or not it exists.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许最好的选择是在服务器端脚本中将允许用户在 30 分钟时间段内进行的尝试次数(基于会话或 IP)限制为合理的限制(即 10 次)。
另一种选择是在页面上放置验证码,以确保用户是人类而不是暴力程序。
Probably the best option is to limit, in the server-side script, the number of attempts a user is allowed to make (based on either session, or IP) to a sensible limit (ie. 10) within a 30 minute time period.
Another option would be to put a captcha on the page to ensure that the user is a human and not a bruteforcing program.
为什么不
Why not
您可以等到表单提交后进行检查,而不是在输入字段失去焦点时进行提示。如果用户名已被使用,一个可靠的解决方案是建议备用用户名。
Instead of prompting when the input field loses focus, you can wait until after the form is submitted to check. If the username is already taken, a robust solution is to suggest alternative user names.
如果您是论坛,则用户名不是私人的。只需使用服务器逻辑来限制每分钟的调用次数。如果你是一家银行,那就另当别论了;但在这种情况下,您可以将用户名分配给银行帐户。
if you're a forum, usernames aren't private. just use server logic to limit the number of calls per minute. if you're a bank, different story; but in that case, you can assign usernames to bank accounts.
我从来没有这样做过,所以只是一个想法。生成一些随表单一起传输的唯一ID(例如uuid)。当您检查用户名时,请包含(并检查)此唯一 ID。当然,这需要一些服务器端的参与,但至少您无法在不渲染表单的情况下直接调用此函数。
注意:为了提高安全性,您需要在每次用户检查值时更新表单唯一 ID。
I've never done this, so just an idea. Generate some unique ID (e.g. uuid) transmitted along with form. When you check for the username include (and check for) this unique ID. Of course, this need some server side involvment, but at least you wouldn't be able directly call this function without rendering the form.
Note: For more security, you would need to update the forms unique ID everytime the user checks for a value.