gpt4 book ai didi

node.js - 使用 Node 和 Express JS 防止暴力破解

转载 作者:IT老高 更新时间:2023-10-28 23:15:08 26 4
gpt4 key购买 nike

我正在使用 Node 和 Express JS 构建一个网站,并希望限制无效的登录尝试。既防止在线破解,又减少不必要的数据库调用。我可以通过哪些方式来实现它?

最佳答案

也许这样的事情可能会帮助您入门。

var failures = {};

function tryToLogin() {
var f = failures[remoteIp];
if (f && Date.now() < f.nextTry) {
// Throttled. Can't try yet.
return res.error();
}

// Otherwise do login
...
}

function onLoginFail() {
var f = failures[remoteIp] = failures[remoteIp] || {count: 0, nextTry: new Date()};
++f.count;
f.nextTry.setTime(Date.now() + 2000 * f.count); // Wait another two seconds for every failed attempt
}

function onLoginSuccess() { delete failures[remoteIp]; }

// Clean up people that have given up
var MINS10 = 600000, MINS30 = 3 * MINS10;
setInterval(function() {
for (var ip in failures) {
if (Date.now() - failures[ip].nextTry > MINS10) {
delete failures[ip];
}
}
}, MINS30);

关于node.js - 使用 Node 和 Express JS 防止暴力破解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com