LoginController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Modules\Admin\Http\Controllers;
  3. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Routing\Controller;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Log;
  8. use Modules\Admin\Services\FilterService;
  9. use Modules\Admin\Services\SettingService;
  10. use DB;
  11. class LoginController extends Controller
  12. {
  13. use AuthenticatesUsers;
  14. /**
  15. * 登陆后的跳转页面
  16. *
  17. * @var string
  18. */
  19. protected $redirectTo = '/admin';
  20. /**
  21. * Create a new controller instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. $this->middleware('guest')->except('logout');
  28. }
  29. //登录页面
  30. public function loginView(Request $request)
  31. {
  32. $filter = FilterService::check_ip();
  33. if (Auth::guard('web')->check()) {
  34. return redirect('/admin');
  35. }
  36. // 获取当前访问者的 IP 地址
  37. $ipAddress = $_SERVER['REMOTE_ADDR'];
  38. // 定义私有 IP 段
  39. $privateRanges = [
  40. ['127.0.0.0', '127.255.255.255'],
  41. ['10.0.0.0', '10.255.255.255'],
  42. ['172.16.0.0', '172.31.255.255'],
  43. ['192.168.0.0', '192.168.255.255']
  44. ];
  45. $ipInPrivateRange = false;
  46. // 判断 IP 是否在任何一个私有 IP 段内
  47. foreach ($privateRanges as $range) {
  48. if ($this->isIpInRange($ipAddress, $range[0], $range[1])) {
  49. $ipInPrivateRange = true;
  50. break;
  51. }
  52. }
  53. if (!$ipInPrivateRange) {
  54. http_response_code(404);
  55. exit;
  56. }
  57. return view('admin::login');
  58. }
  59. function isIpInRange($ip, $rangeStart, $rangeEnd) {
  60. $ipLong = ip2long($ip);
  61. $startLong = ip2long($rangeStart);
  62. $endLong = ip2long($rangeEnd);
  63. return ($ipLong >= $startLong && $ipLong <= $endLong);
  64. }
  65. //登录动作
  66. public function doLogin(Request $request)
  67. {
  68. $request['password'] = base64_decode($request->input('password'));
  69. $this->validateLogin($request);
  70. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  71. // the login attempts for this application. We'll key this by the username and
  72. // the IP address of the client making these requests into this application.
  73. if (method_exists($this, 'hasTooManyLoginAttempts') &&
  74. $this->hasTooManyLoginAttempts($request)) {
  75. $this->fireLockoutEvent($request);
  76. return $this->sendLockoutResponse($request);
  77. }
  78. $valid = validPass($request['password']);
  79. if (is_string($valid)) {
  80. $this->guard()->logout();
  81. $request->session()->invalidate();
  82. return [
  83. 'code' => 1,
  84. 'message' => '密码限制:' . $valid . ',请找管理员修改',
  85. ];
  86. }
  87. if ($this->attemptLogin($request)) {
  88. // $roles = $this->guard()->user()->getRoleNames()->toArray();
  89. // if (count($roles) == 0 || (count($roles) == 1 && $roles[0] == 'PuTongZhiYuan')) {
  90. // $this->guard()->logout();
  91. // $request->session()->invalidate();
  92. // return [
  93. // 'code' => 1,
  94. // 'message' => '没有登录权限',
  95. // ];
  96. // }
  97. $token = $request->input('staff_num') . "," . $request->input('password') . "," . $request->getClientIp();
  98. $token = base64_encode($token);
  99. setcookie("token", $token, time() + 7200, "/", ".nxjiewei.com");
  100. return $this->sendLoginResponse($request);
  101. }
  102. // If the login attempt was unsuccessful we will increment the number of attempts
  103. // to login and redirect the user back to the login form. Of course, when this
  104. // user surpasses their maximum number of attempts they will get locked out.
  105. $this->incrementLoginAttempts($request);
  106. return $this->sendFailedLoginResponse($request);
  107. }
  108. protected function sendLoginResponse(Request $request)
  109. {
  110. $request->session()->regenerate();
  111. $this->clearLoginAttempts($request);
  112. return [
  113. 'code' => 0,
  114. 'message' => '登录成功',
  115. ];
  116. }
  117. /**
  118. * 用户名验证字段
  119. * @return string
  120. */
  121. public function username()
  122. {
  123. return 'staff_num';
  124. }
  125. }