LoginValidate.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\adminapi\validate;
  15. use app\common\enum\AdminTerminalEnum;
  16. use app\common\model\auth\Admin;
  17. use app\common\cache\AdminAccountSafeCache;
  18. use app\common\service\ConfigService;
  19. use app\common\validate\BaseValidate;
  20. use think\facade\Config;
  21. /**
  22. * 登录验证
  23. * Class LoginValidate
  24. * @package app\adminapi\validate
  25. */
  26. class LoginValidate extends BaseValidate
  27. {
  28. protected $rule = [
  29. 'terminal' => 'require|in:' . AdminTerminalEnum::PC . ',' . AdminTerminalEnum::MOBILE,
  30. 'account' => 'require',
  31. 'password' => 'require|password',
  32. ];
  33. protected $message = [
  34. 'account.require' => '请输入账号',
  35. 'password.require' => '请输入密码'
  36. ];
  37. /**
  38. * @notes @notes 密码验证
  39. * @param $password
  40. * @param $other
  41. * @param $data
  42. * @return bool|string
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @author 令狐冲
  47. * @date 2021/7/2 14:00
  48. */
  49. public function password($password, $other, $data)
  50. {
  51. // 登录限制
  52. $config = [
  53. 'login_restrictions' => ConfigService::get('admin_login', 'login_restrictions'),
  54. 'password_error_times' => ConfigService::get('admin_login', 'password_error_times'),
  55. 'limit_login_time' => ConfigService::get('admin_login', 'limit_login_time'),
  56. ];
  57. $adminAccountSafeCache = new AdminAccountSafeCache();
  58. if ($config['login_restrictions'] == 1) {
  59. $adminAccountSafeCache->count = $config['password_error_times'];
  60. $adminAccountSafeCache->minute = $config['limit_login_time'];
  61. }
  62. //后台账号安全机制,连续输错后锁定,防止账号密码暴力破解
  63. if ($config['login_restrictions'] == 1 && !$adminAccountSafeCache->isSafe()) {
  64. return '密码连续' . $adminAccountSafeCache->count . '次输入错误,请' . $adminAccountSafeCache->minute . '分钟后重试';
  65. }
  66. $adminInfo = Admin::where('account', '=', $data['account'])
  67. ->field(['password,disable'])
  68. ->findOrEmpty();
  69. if ($adminInfo->isEmpty()) {
  70. return '账号不存在';
  71. }
  72. if ($adminInfo['disable'] === 1) {
  73. return '账号已禁用';
  74. }
  75. if (empty($adminInfo['password'])) {
  76. $adminAccountSafeCache->record();
  77. return '账号不存在';
  78. }
  79. $passwordSalt = Config::get('project.unique_identification');
  80. if ($adminInfo['password'] !== create_password($password, $passwordSalt)) {
  81. $adminAccountSafeCache->record();
  82. return '密码错误';
  83. }
  84. $adminAccountSafeCache->relieve();
  85. return true;
  86. }
  87. }