有的项目客户端为了保证用户数据安全,涉及到敏感操作,会有严格的错误次数限制,比如用户多次登录失败即锁定客户端ip,防止爆破,这个用redis就可以实现(其他kv数据库也行),细节如下:
示例在eggjs
下实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| async ipInspect(body) { const { accumulate = true } = body || {} let ip = this.ctx.request.get('X-Real-IP') if (this.config.env === 'local') { ip = this.ctx.ip.includes('::1') ? '127.0.0.1' : this.ctx.ip } const name = this.app.config.name let key = `${name}_${ip}` let lockSeconds = 60 * 5 let lock_key = `${name}_dura_${ip}` const count = await this.app.redis.get(key) let lock_ttl = await this.app.redis.ttl(lock_key)
if (count && count >= 4 && lock_ttl < -1) { await this.app.redis.set(lock_key, 1, 'EX', lockSeconds) lock_ttl = lockSeconds } if (lock_ttl >= -1) { const remain_time = Math.ceil(lock_ttl / 60) return remain_time } if (accumulate) { await this.app.redis.incr(key) await this.app.redis.expire(key, 60 * 1) } return false }
|