LoginController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. ['10.0.0.0', '10.255.255.255'],
  41. ['172.16.0.0', '172.31.255.255'],
  42. ['192.168.0.0', '192.168.255.255']
  43. ];
  44. $ipInPrivateRange = false;
  45. // 判断 IP 是否在任何一个私有 IP 段内
  46. foreach ($privateRanges as $range) {
  47. if ($this->isIpInRange($ipAddress, $range[0], $range[1])) {
  48. $ipInPrivateRange = true;
  49. break;
  50. }
  51. }
  52. if (!$ipInPrivateRange) {
  53. http_response_code(404);
  54. exit;
  55. }
  56. return view('admin::login');
  57. }
  58. function isIpInRange($ip, $rangeStart, $rangeEnd) {
  59. $ipLong = ip2long($ip);
  60. $startLong = ip2long($rangeStart);
  61. $endLong = ip2long($rangeEnd);
  62. return ($ipLong >= $startLong && $ipLong <= $endLong);
  63. }
  64. //登录动作
  65. public function doLogin(Request $request)
  66. {
  67. $request['password'] = base64_decode($request->input('password'));
  68. $this->validateLogin($request);
  69. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  70. // the login attempts for this application. We'll key this by the username and
  71. // the IP address of the client making these requests into this application.
  72. if (method_exists($this, 'hasTooManyLoginAttempts') &&
  73. $this->hasTooManyLoginAttempts($request)) {
  74. $this->fireLockoutEvent($request);
  75. return $this->sendLockoutResponse($request);
  76. }
  77. $valid = validPass($request['password']);
  78. if (is_string($valid)) {
  79. $this->guard()->logout();
  80. $request->session()->invalidate();
  81. return [
  82. 'code' => 1,
  83. 'message' => '密码限制:' . $valid . ',请找管理员修改',
  84. ];
  85. }
  86. if ($this->attemptLogin($request)) {
  87. // $roles = $this->guard()->user()->getRoleNames()->toArray();
  88. // if (count($roles) == 0 || (count($roles) == 1 && $roles[0] == 'PuTongZhiYuan')) {
  89. // $this->guard()->logout();
  90. // $request->session()->invalidate();
  91. // return [
  92. // 'code' => 1,
  93. // 'message' => '没有登录权限',
  94. // ];
  95. // }
  96. $token = $request->input('staff_num') . "," . $request->input('password') . "," . $request->getClientIp();
  97. $token = base64_encode($token);
  98. setcookie("token", $token, time() + 7200, "/", ".nxjiewei.com");
  99. return $this->sendLoginResponse($request);
  100. }
  101. // If the login attempt was unsuccessful we will increment the number of attempts
  102. // to login and redirect the user back to the login form. Of course, when this
  103. // user surpasses their maximum number of attempts they will get locked out.
  104. $this->incrementLoginAttempts($request);
  105. return $this->sendFailedLoginResponse($request);
  106. }
  107. protected function sendLoginResponse(Request $request)
  108. {
  109. $request->session()->regenerate();
  110. $this->clearLoginAttempts($request);
  111. return [
  112. 'code' => 0,
  113. 'message' => '登录成功',
  114. ];
  115. }
  116. /**
  117. * 用户名验证字段
  118. * @return string
  119. */
  120. public function username()
  121. {
  122. return 'staff_num';
  123. }
  124. }