isSecurity.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Components\IP;
  4. use Cache;
  5. use Closure;
  6. use Log;
  7. use Response;
  8. class isSecurity
  9. {
  10. /**
  11. * 是否需要安全码才访问(仅用于登录页)
  12. *
  13. * @param $request
  14. * @param Closure $next
  15. *
  16. * @return mixed
  17. */
  18. public function handle($request, Closure $next)
  19. {
  20. $ip = IP::getClientIP();
  21. $code = $request->securityCode;
  22. $cacheKey = 'SecurityLogin_' . ip2long($ip);
  23. $websiteSecurityCode = sysConfig('website_security_code');
  24. if ($websiteSecurityCode && ! Cache::has($cacheKey)) {
  25. if ($code != $websiteSecurityCode) {
  26. Log::info("拒绝非安全入口访问(" . $ip . ")");
  27. return Response::view(
  28. 'auth.error',
  29. [
  30. 'message' => trans('error.SecurityError') . ',
  31. ' . trans(
  32. 'error.Visit'
  33. ) . '<a href="/login?securityCode=" target="_self">' .
  34. trans('error.SecurityEnter') . '</a>',
  35. ],
  36. 403
  37. );
  38. }
  39. Cache::put($cacheKey, $ip, 7200); // 2小时之内无需再次输入安全码访问
  40. }
  41. return $next($request);
  42. }
  43. }