UserAccountSafeCache.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\cache;
  15. /**
  16. * //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
  17. * Class AdminAccountSafeCache
  18. * @package app\common\cache
  19. */
  20. class UserAccountSafeCache extends BaseCache
  21. {
  22. private $key;//缓存次数名称
  23. public $minute = 15;//缓存设置为15分钟,即密码错误次数达到,锁定15分钟
  24. public $count = 15; //设置连续输错次数,即15分钟内连续输错误15次后,锁定
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $ip = \request()->ip();
  29. $this->key = $this->tagName . $ip;
  30. }
  31. /**
  32. * @notes 记录登录错误次数
  33. * @author 令狐冲
  34. * @date 2021/6/30 01:51
  35. */
  36. public function record()
  37. {
  38. if ($this->get($this->key)) {
  39. //缓存存在,记录错误次数
  40. $this->inc($this->key, 1);
  41. } else {
  42. //缓存不存在,第一次设置缓存
  43. $this->set($this->key, 1, $this->minute * 60);
  44. }
  45. }
  46. /**
  47. * @notes 判断是否安全
  48. * @return bool
  49. * @author 令狐冲
  50. * @date 2021/6/30 01:53
  51. */
  52. public function isSafe()
  53. {
  54. $count = $this->get($this->key);
  55. if ($count >= $this->count) {
  56. return false;
  57. }
  58. return true;
  59. }
  60. /**
  61. * @notes 删除该ip记录错误次数
  62. * @author 令狐冲
  63. * @date 2021/6/30 01:55
  64. */
  65. public function relieve()
  66. {
  67. $this->delete($this->key);
  68. }
  69. }