middleware('guest')->except('logout'); } //登录页面 public function loginView(Request $request) { $filter = FilterService::check_ip(); if (Auth::guard('web')->check()) { return redirect('/admin'); } // 获取当前访问者的 IP 地址 $ipAddress = $_SERVER['REMOTE_ADDR']; // 定义私有 IP 段 $privateRanges = [ ['10.0.0.0', '10.255.255.255'], ['172.16.0.0', '172.31.255.255'], ['192.168.0.0', '192.168.255.255'] ]; $ipInPrivateRange = false; // 判断 IP 是否在任何一个私有 IP 段内 foreach ($privateRanges as $range) { if ($this->isIpInRange($ipAddress, $range[0], $range[1])) { $ipInPrivateRange = true; break; } } if (!$ipInPrivateRange) { http_response_code(404); exit; } return view('admin::login'); } function isIpInRange($ip, $rangeStart, $rangeEnd) { $ipLong = ip2long($ip); $startLong = ip2long($rangeStart); $endLong = ip2long($rangeEnd); return ($ipLong >= $startLong && $ipLong <= $endLong); } //登录动作 public function doLogin(Request $request) { $request['password'] = base64_decode($request->input('password')); $this->validateLogin($request); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } $valid = validPass($request['password']); if (is_string($valid)) { $this->guard()->logout(); $request->session()->invalidate(); return [ 'code' => 1, 'message' => '密码限制:' . $valid . ',请找管理员修改', ]; } if ($this->attemptLogin($request)) { // $roles = $this->guard()->user()->getRoleNames()->toArray(); // if (count($roles) == 0 || (count($roles) == 1 && $roles[0] == 'PuTongZhiYuan')) { // $this->guard()->logout(); // $request->session()->invalidate(); // return [ // 'code' => 1, // 'message' => '没有登录权限', // ]; // } $token = $request->input('staff_num') . "," . $request->input('password') . "," . $request->getClientIp(); $token = base64_encode($token); setcookie("token", $token, time() + 7200, "/", ".nxjiewei.com"); return $this->sendLoginResponse($request); } // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); } protected function sendLoginResponse(Request $request) { $request->session()->regenerate(); $this->clearLoginAttempts($request); return [ 'code' => 0, 'message' => '登录成功', ]; } /** * 用户名验证字段 * @return string */ public function username() { return 'staff_num'; } }