isSecurity.php 1.4 KB

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