KefuAccountSafeCache.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\cache;
  20. /**
  21. * 客服账号登录安全机制,连续输错后锁定,防止账号密码暴力破解
  22. * Class KefuAccountSafeCache
  23. * @package app\common\cache
  24. */
  25. class KefuAccountSafeCache extends BaseCache
  26. {
  27. private $key;//缓存次数名称
  28. public $minute = 15;//缓存设置为15分钟,即密码错误次数达到,锁定15分钟
  29. public $count = 15; //设置连续输错次数,即15分钟内连续输错误15次后,锁定
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $ip = \request()->ip();
  34. $this->key = $this->tagName . $ip;
  35. }
  36. /**
  37. * @notes 记录登录错误次数
  38. * @author 段誉
  39. * @date 2022/3/9 18:59
  40. */
  41. public function record()
  42. {
  43. if ($this->get($this->key)) {
  44. //缓存存在,记录错误次数
  45. $this->inc($this->key, 1);
  46. } else {
  47. //缓存不存在,第一次设置缓存
  48. $this->set($this->key, 1, $this->minute * 60);
  49. }
  50. }
  51. /**
  52. * @notes 判断是否安全
  53. * @return bool
  54. * @author 段誉
  55. * @date 2022/3/9 18:59
  56. */
  57. public function isSafe()
  58. {
  59. $count = $this->get($this->key);
  60. if ($count >= $this->count) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * @notes 删除该ip记录错误次数
  67. * @author 段誉
  68. * @date 2022/3/9 19:00
  69. */
  70. public function relieve()
  71. {
  72. $this->delete($this->key);
  73. }
  74. }